0
0
Kotlinprogramming~10 mins

Sealed classes for restricted hierarchies in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sealed classes for restricted hierarchies
Define sealed class
Define subclasses inside or outside
Use when expression to check subclass
Handle all subclasses exhaustively
No other subclasses allowed outside hierarchy
End
Sealed classes restrict subclassing to a known set, enabling exhaustive checks in when expressions.
Execution Sample
Kotlin
sealed class Shape {
    data class Circle(val radius: Double) : Shape()
    data class Rectangle(val width: Double, val height: Double) : Shape()
}

fun area(shape: Shape): Double = when(shape) {
    is Shape.Circle -> 3.14 * shape.radius * shape.radius
    is Shape.Rectangle -> shape.width * shape.height
}
Calculates area for Circle or Rectangle using sealed class Shape and exhaustive when.
Execution Table
StepActionInputConditionBranch TakenOutput
1Call area with Circle(radius=2.0)Circle(radius=2.0)shape is Circle?YesArea = 3.14 * 2.0 * 2.0 = 12.56
2Return area for Circle12.56
3Call area with Rectangle(width=3.0, height=4.0)Rectangle(width=3.0, height=4.0)shape is Circle?No
4Check if shape is RectangleRectangle(width=3.0, height=4.0)shape is Rectangle?YesArea = 3.0 * 4.0 = 12.0
5Return area for Rectangle12.0
6Call area with unknown subclass (not allowed)UnknownShape()No matching subclassErrorCompilation error: sealed class restricts subclasses
💡 Execution stops because all subclasses are handled; no other subclasses allowed outside sealed class.
Variable Tracker
VariableStartAfter 1After 2After 3Final
shape-Circle(radius=2.0)Rectangle(width=3.0, height=4.0)UnknownShape()-
area-12.5612.0Error-
Key Moments - 3 Insights
Why does the when expression not need an else branch?
Because the sealed class restricts subclasses, the when expression covers all cases explicitly (see execution_table rows 1-5).
Can we create a subclass of the sealed class outside its file?
No, sealed classes only allow subclasses defined in the same file, preventing unknown subclasses (see execution_table row 6).
What happens if a new subclass is added but not handled in when?
The compiler forces you to handle it or add an else branch, ensuring exhaustive checks.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the area output when shape is Circle(radius=2.0)?
A3.14
B12.56
C4.0
DError
💡 Hint
Check execution_table row 1 and 2 for Circle input and output.
At which step does the when expression handle the Rectangle subclass?
AStep 1
BStep 3
CStep 4
DStep 6
💡 Hint
Look at execution_table rows 3 and 4 for Rectangle handling.
If a new subclass is added outside the sealed class file, what happens?
ACompiler error due to sealed class restrictions
BIt compiles and runs normally
CIt runs but when expression skips it
DIt requires an else branch
💡 Hint
See execution_table row 6 about unknown subclass and error.
Concept Snapshot
sealed class ClassName {
  // subclasses defined here or same file
}

Use when to check all subclasses without else.
Compiler ensures no other subclasses exist.
Enables safe, exhaustive type checks.
Full Transcript
Sealed classes in Kotlin let you define a restricted set of subclasses. You declare a sealed class and its subclasses in the same file. This restriction helps the compiler know all possible subclasses. When you use a when expression on a sealed class, you can check all subclasses explicitly without needing an else branch. The compiler ensures you handle every subclass, making your code safer and clearer. If you try to create a subclass outside the sealed class file, the compiler will give an error. This way, sealed classes help you create restricted hierarchies with exhaustive checks.