100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Kotlin Sealed Classes Cheat Sheet

Kotlin Sealed Classes Cheat Sheet

Covers declaring sealed classes/interfaces, exhaustive when expressions, sealed hierarchies for state modeling, and common Android/Compose patterns.

2 PagesIntermediateFeb 10, 2026

Declaring a Sealed Class

Restrict a type hierarchy to a fixed set of subtypes known at compile time.

kotlin
sealed class Result<out T>data class Success<T>(val data: T) : Result<T>()data class Error(val message: String, val cause: Throwable? = null) : Result<Nothing>()object Loading : Result<Nothing>()// Kotlin 1.9+: sealed interfaces work the same waysealed interface UiStatedata class Content(val items: List<String>) : UiStatedata object Empty : UiState   // 'data object' since 1.9 gives toString/equals for freedata object Loading2 : UiState

Exhaustive `when` Expressions

The compiler forces you to handle every subtype, no `else` needed.

kotlin
fun render(result: Result<String>): String = when (result) {    is Success -> "Data: ${result.data}"    is Error   -> "Failed: ${result.message}"    Loading    -> "Loading..."    // no `else` branch required — compiler errors if a case is missing}// Adding a new subclass later triggers a compile error at every `when`// that isn't updated — this is the main safety win over open classes.

Nested & Local Subclasses

Sealed subclasses can be nested inside the parent or declared in the same file/package (Kotlin 1.5+).

kotlin
sealed class NetworkResponse {    sealed class Success : NetworkResponse() {        data class Ok(val body: String) : Success()        object NoContent : Success()    }    data class Failure(val code: Int) : NetworkResponse()}fun handle(r: NetworkResponse) = when (r) {    is NetworkResponse.Success.Ok -> println(r.body)    NetworkResponse.Success.NoContent -> println("204")    is NetworkResponse.Failure -> println("error ${r.code}")}

Sealed Class vs Enum vs Sealed Interface

Quick reference for choosing the right construct.

  • Enum class- fixed set of singleton instances, no per-case state or generics
  • Sealed class- fixed set of subtypes that CAN carry different data/state per case
  • Sealed interface- like sealed class but subtypes can also extend other classes
  • data object- Kotlin 1.9+ singleton sealed subtype with generated equals/toString
  • abstract class- open hierarchy, compiler can't enforce exhaustive when
  • Cross-module sealed- subclasses must be in same module + package as of Kotlin 1.5
Pro Tip

Use sealed classes to model network/UI state (Loading/Success/Error) instead of nullable flags or booleans — it makes invalid states like 'loading AND has data' unrepresentable at compile time.

Was this cheat sheet helpful?

Explore Topics

#KotlinSealedClasses#KotlinSealedClassesCheatSheet#Programming#Intermediate#DeclaringASealedClass#ExhaustiveWhenExpressions#NestedLocalSubclasses#Sealed#OOP#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet