Sealed Class vs Enum in Kotlin: Key Differences and Usage
enum defines a fixed set of constants with simple behavior, while sealed class allows a restricted class hierarchy with multiple types and custom data. Use enum for fixed options and sealed class for complex, type-safe hierarchies.Quick Comparison
Here is a quick side-by-side comparison of sealed class and enum in Kotlin.
| Factor | Enum | Sealed Class |
|---|---|---|
| Purpose | Fixed set of constants | Restricted class hierarchy with multiple types |
| Data | Can hold properties but limited | Can hold different properties per subclass |
| Inheritance | Cannot be subclassed | Can have subclasses |
| Use case | Simple states or options | Complex states with different data |
| When to use | Known fixed values | Type-safe polymorphism |
| Extensibility | Closed and fixed | Closed but flexible with subclasses |
Key Differences
Enum in Kotlin is designed to represent a fixed set of constants. Each enum entry is a singleton object, and enums cannot be subclassed. They are simple and useful when you have a known list of options, like days of the week or directions.
On the other hand, sealed class lets you create a restricted class hierarchy where all subclasses are known at compile time but can hold different data and behavior. This makes sealed classes perfect for representing complex states or results with different data types, like success or error responses.
While enums are limited to simple values and shared properties, sealed classes allow each subclass to have its own properties and methods. This flexibility makes sealed classes a powerful tool for type-safe polymorphism and exhaustive when expressions.
Code Comparison
Here is how you can represent a simple state with enum in Kotlin:
enum class Direction { NORTH, SOUTH, EAST, WEST } fun describeDirection(direction: Direction): String { return when(direction) { Direction.NORTH -> "Going north" Direction.SOUTH -> "Going south" Direction.EAST -> "Going east" Direction.WEST -> "Going west" } } fun main() { println(describeDirection(Direction.EAST)) }
Sealed Class Equivalent
Here is the same concept using a sealed class to represent directions with more flexibility:
sealed class Direction { object North : Direction() object South : Direction() object East : Direction() object West : Direction() } fun describeDirection(direction: Direction): String { return when(direction) { Direction.North -> "Going north" Direction.South -> "Going south" Direction.East -> "Going east" Direction.West -> "Going west" } } fun main() { println(describeDirection(Direction.East)) }
When to Use Which
Choose enum when you have a simple, fixed set of constants without extra data or behavior. Enums are easy to use and perfect for options like days, colors, or states.
Choose sealed class when you need a type-safe hierarchy with different data or behavior per type. Sealed classes shine in representing complex states, results, or events where each subclass can hold unique information.
In summary, use enums for simple fixed sets and sealed classes for flexible, structured data types.
Key Takeaways
enum for fixed sets of simple constants with shared behavior.sealed class for complex, type-safe hierarchies with different data per subclass.when expressions with custom data.