Model data with Kotlin sealed classes and exhaustive when
10 steps · about 3 h 45 min · Intermediate
- Kotlin Course - Tutorial for Beginners (video, YouTube (freeCodeCamp), 40 min)
Start with the basics of classes, enums, sealed classes and data classes, using the chapter markers to jump to the parts you need.
- Conditions and loops (doc, Kotlin Help (JetBrains), 15 min)
It explains when as an expression, why expressions must cover every case, and when you can drop else.
- Enum classes (doc, Kotlin Help (JetBrains), 15 min)
Enums are the simplest closed set of values, and the page shows where sealed types take over.
- Kotlin Koans (interactive, Kotlin Playground (JetBrains), 45 min)
Practise classes and conventions in the browser before you design your own hierarchies.
- Sealed classes and interfaces (doc, Kotlin Help (JetBrains), 25 min)
This is the official reference for sealed rules, subclasses and exhaustive when, so read it carefully.
- Kotlin Sealed Keyword - Restricted Class Hierarchies (tutorial, ZetCode, 20 min)
Short worked examples of sealed classes and interfaces, including nested hierarchies, help the rules stick.
- Guide to using sealed classes in Kotlin (article, LogRocket Blog, 20 min)
It shows the compiler error you get when a branch is missing, so you see exhaustiveness checking at work.
- How to Use Sealed Classes for Type-Safe State in Kotlin (article, OneUptime, 20 min)
Apply sealed types to a common real task, modelling UI and loading state.
- Effective Kotlin Item 39: Use sealed classes and interfaces to express restricted hierarchies (article, Kt. Academy, 15 min)
Learn the design trade-offs, including why adding a subtype to a public API is a breaking change.
- Inheritance - Kotlin language specification (reference, Kotlin language specification, 10 min)
Finish with the formal rules on sealed inheritance, to check your mental model against the spec.