Mobile development

Stage 1 · Lesson 5 of 9

Sealed classes and when

Kotlin fundamentals

A or sealed interface has a fixed set of subtypes that the compiler knows about, such as Loading, Success(data) and Error(message). A when expression over it must cover every subtype. So if you add a new state, the compiler points at each place you need to update.

Use an enum class when the options carry no data of their own, such as a sort order. Use a sealed type when each case holds its own fields. This is the standard way to model UI state in Android apps, and you will use it in the next stages.

Common trapa with isLoading, data and error fields that allows impossible combinations.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • GuideFree
    Kotlin Documentation

    JetBrains

    Start with the Kotlin tour, then the null safety and coroutines basics pages

  • CourseFree
    Android Basics with Compose

    Google · Beginner

    The Kotlin pathways in Units 1 to 3 teach the language with practice sets

Practise

  • Dart, TypeScript, Kotlin and Swift tracks with small exercises and free mentor feedback

Keep handy

  • Short Kotlin pages with a try-it editor for checking syntax quickly

Also worth your time

Mark this stage as not done?