0
0
KotlinComparisonBeginner · 4 min read

Sealed Class vs Enum in Kotlin: Key Differences and Usage

In Kotlin, 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.

FactorEnumSealed Class
PurposeFixed set of constantsRestricted class hierarchy with multiple types
DataCan hold properties but limitedCan hold different properties per subclass
InheritanceCannot be subclassedCan have subclasses
Use caseSimple states or optionsComplex states with different data
When to useKnown fixed valuesType-safe polymorphism
ExtensibilityClosed and fixedClosed 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:

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))
}
Output
Going east
↔️

Sealed Class Equivalent

Here is the same concept using a sealed class to represent directions with more flexibility:

kotlin
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))
}
Output
Going 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

Use enum for fixed sets of simple constants with shared behavior.
Use sealed class for complex, type-safe hierarchies with different data per subclass.
Enums cannot be subclassed; sealed classes allow multiple subclasses.
Sealed classes enable exhaustive when expressions with custom data.
Choose based on complexity: enums for simple options, sealed classes for rich states.