Problem Statement
What is the purpose of sealed classes in Kotlin?
Explanation
Sealed classes define restricted class hierarchies where all subclasses must be declared in the same file or as nested classes, enabling the compiler to know all possible subclasses at compile time. This is extremely useful for representing finite state machines, result types with success and error cases, or UI states where you want exhaustive when expressions.
The compiler can verify exhaustiveness of when expressions over sealed class hierarchies, warning you when you haven't handled all cases without needing an else branch. Sealed classes are abstract by default and their constructors are private, combining benefits of enums with the flexibility of allowing subclasses to hold different data and have multiple instances.
