Introduction
A sealed class is a special kind of abstract class that restricts which classes can inherit from it. All direct subclasses of a sealed class must be defined in the same file or module, so the compiler knows the complete set of possible subtypes at compile time. This makes sealed classes ideal for representing restricted states, such as the result of an operation that can only be Loading, Success, or Error. The sealed modifier marks the base class as having a closed hierarchy. Subclasses can be regular classes, data classes, or objects (for states without data). Because the compiler can see every subclass, a when expression over a sealed class value can be exhaustive without requiring an else branch, and the compiler will emit an error if a new subclass is added but not handled somewhere the hierarchy is checked.
Cricket analogy: A sealed class MatchResult with only Win, Loss, and Tie subclasses, all declared in the same file, lets the scorer's when block handle every possible outcome exhaustively without an else branch, since no fourth result can secretly exist.
Syntax
sealed class Result {
class Success(val data: String) : Result()
class Error(val message: String) : Result()
object Loading : Result()
}Example
fun handle(result: Result): String = when (result) {
is Result.Success -> "Data: ${result.data}"
is Result.Error -> "Error: ${result.message}"
Result.Loading -> "Loading..."
}
fun main() {
println(handle(Result.Success("42")))
println(handle(Result.Error("Not found")))
println(handle(Result.Loading))
}Output
Data: 42
Error: Not found
Loading...Key Takeaways
- Sealed classes restrict inheritance to a known, closed set of subclasses in the same file/module.
- They enable exhaustive
whenexpressions without anelsebranch. - Subclasses can be classes, data classes, or objects.
- Ideal for modeling restricted states like Loading/Success/Error.
- The compiler flags unhandled subclasses in
whenblocks, improving safety.
Practice what you learned
1. What is the main benefit of using a sealed class over a regular abstract class?
2. Where must direct subclasses of a sealed class be defined?
3. Can a sealed class be instantiated directly?
4. Which of these can be a subclass of a sealed class?
5. What happens if you add a new subclass to a sealed class but forget to handle it in an existing exhaustive when?
Was this page helpful?
You May Also Like
when Expression in Kotlin
Explore Kotlin's when expression, a powerful and flexible replacement for Java's switch statement.
Enum Classes in Kotlin
Enum classes define a fixed set of singleton constants that can carry properties and methods.
Abstract Classes in Kotlin
Abstract classes provide a partial implementation with both abstract and concrete members that subclasses complete.
Data Classes in Kotlin
See how Kotlin data classes auto-generate equals, hashCode, toString, copy, and componentN functions for value-holding objects.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics