Originally published on Medium:
https://medium.com/@supsabhi/i-was-using-sealed-classes-wrong-in-android-for-two-years-heres-what-nobody-told-me-3daa5cbdcbca?sharedUserId=supsabhi
A beginner-friendly story about one of Kotlin’s most misunderstood features — and why the distinction finally matters.
We’ve all been there…Okay, real talk.
When I first started Android development, while developing a sleek new feature to handle different states, I thought, “Okay, I’ll just use an Enum.” But then, reality hits. Success state needs to carry a User object, while Error state needs an Exception. Enums start sweating. They can’t do that.
And then I came across the term sealed class and thought, "Cool. It's like an enum, but fancier." I used it everywhere. State management? Sealed class. Network results? Sealed class. UI events? Sealed class.
Then Kotlin 1.5 quietly introduced sealed interfaces and I thought, “Why would I ever need that? I already have sealed classes.”
I was wrong. For two years.
This article is the one I wish someone had handed me back then. Let’s fix this together — no jargon walls, no theory dumps.
First, Let’s Talk About What “Sealed” Even Means
Think of sealed classes like a bouncer at an exclusive club 🚪
The bouncer has a strict guest list. Only people on that list are allowed in — no walk-ins, no surprises. If your name isn’t on the list, you don’t get to be a subtype.
That’s what sealed means in Kotlin.
A sealed type restricts who can extend it. Only classes/objects defined in the same file (or package in newer Kotlin) can be its subtypes.
You define exactly who is allowed in (the subclasses) at compile time. This gives you something incredibly powerful: when you write a when expression on a sealed type, Kotlin knows every possible case. No surprises. No mystery subclasses coming from the outside world.
The Example
A sealed class is a class that can hold state. It’s like a data container with a restricted family tree.
Let’s look at a real Android use case: UI State management.
Imagine you’re building a screen that loads a list of movies. At any given moment, your screen is in one of these states:
sealed class MovieState {
object Loading : MovieState()
data class Success(val movies: List<Movie>) : MovieState()
data class Error(val message: String) : MovieState()
}
when (val state = viewModel.state.collectAsState().value) {
is MovieState.Loading -> LoadingSpinner()
is MovieState.Success -> MovieList(movies = state.movies)
is MovieState.Error -> ErrorMessage(text = state.message)
// No `else` needed! Kotlin knows these are ALL the cases.
}
That // No else needed comment? That's the magic. If you add a new state later and forget to handle it in when, the compiler yells at you. It's like having a very helpful, slightly annoying coworker who catches your mistakes before users do.
Sealed classes are clean, readable, and safe. No nulls flying around, no magic strings, no mystery states.
But Here’s Where Sealed Classes Hit a Wall
Let me tell you about the day sealed classes broke my heart a little.
I was building an app where I needed to represent user actions — things the user could do on a profile screen. Some actions were general (like Share), and some were specific (like Follow for social profiles, Buy for shop profiles).
I thought: “Easy! Sealed class it is.”
sealed class ProfileAction {
object Share : ProfileAction()
object Follow : ProfileAction() // Only for social profiles
object Buy : ProfileAction() // Only for shop profiles
}
Then I wanted to add a second layer — maybe these actions also need to conform to an Analyticsable interface (so I can track them), and an Undoable interface (so the user can undo them).
And here’s where I hit the wall.
A class can only extend ONE class. So if ProfileAction is a sealed class, none of my subtypes can extend anything else. They're stuck.
This is not a Kotlin problem. This is just how inheritance works everywhere.
Enter: Sealed Interfaces
Kotlin 1.5 introduced sealed interfaces, and they solve exactly this problem.
An interface doesn’t hold state by itself — it’s a contract, a set of promises. And crucially, a class can implement multiple interfaces.
Sealed interfaces are the “cool younger sibling.” They do exactly what sealed classes do — restrict the hierarchy — but with one massive superpower: Multiple Inheritance.
Let me rewrite the example:
sealed interface ProfileAction
// Interfaces our actions can also conform to
interface Analyticsable {
val eventName: String
}
interface Undoable {
fun undo()
}
Now look at this beauty:
object Share : ProfileAction, Analyticsable {
override val eventName = "share_tapped"
}
object Follow : ProfileAction, Analyticsable, Undoable {
override val eventName = "follow_tapped"
override fun undo() { /* unfollow logic */ }
}
data class Buy(val productId: String) : ProfileAction, Analyticsable {
override val eventName = "buy_tapped"
}
Each action can mix in only the behaviors it needs. Share can be tracked. Follow can be tracked AND undone. Buy holds data AND can be tracked.
And when you use when:
fun handleAction(action: ProfileAction) {
when (action) {
is Share -> shareContent()
is Follow -> followUser()
is Buy -> openCheckout(action.productId)
}
}
Still exhaustive. Still safe. Way more flexible.
So… When Do I Use Which?
Let me be honest — for 80% of everyday Android use cases, sealed classes will serve you just fine. Don’t overthink it.
Here’s my personal mental model:
Reach for a Sealed Class when:
- You’re modeling UI states (Loading, Success, Error)
- You’re modeling network/repository results
- Your subtypes need constructor properties and you want everything in one tidy place
- You’re not combining behaviors from multiple type hierarchies
Reach for a Sealed Interface when:
- Your subtypes need to implement other interfaces too
- You’re building a plugin-style or capability-based hierarchy
- You want a subtype that also extends another class
- You’re working on a multi-module project where different modules contribute subtypes (Kotlin 1.5+ allows this with interfaces across packages)
The Golden Rule of Thumb 💡
- Do I need to pass a variable to the parent? (e.g., sealed class Error(val code: Int)). If yes → use a Sealed Class.
- Does my subclass need to belong to multiple categories? If yes → use a Sealed Interface.
- Are you defining a simple state with no shared logic, but your subtypes need to mix in other behaviors? → use a Sealed Interface. It gives you the flexibility to grow.
Wrapping Up
In the modern Android world (especially with Jetpack Compose), you’ll find yourself reaching for sealed interfaces more often — for things like NavigationEvents or UiActions — because of their flexibility. However, for a robust ViewState that might need some base properties, the sealed class remains your best friend.
You don’t need to refactor your entire codebase tonight. But next time you’re designing a hierarchy and think “I wish this subtype could also be X…” — remember sealed interfaces have your back.
The compiler is your friend. Use sealed types, and let it do the heavy lifting of telling you when something’s missing or broken.
That’s what Kotlin is all about.
If this helped you, drop a clap (or 50 — Medium lets you!) and share it within your team who’s been using sealed classes without questioning it. You might just save them two years. 🙂
Happy coding. 🚀

Top comments (1)
This is the exact wall I hit too, Supriya — the ProfileAction example nails it. A sealed class is perfect while every subtype only needs the parent's state, and the moment one subtype wants a second capability, the single-inheritance ceiling shows up. Your Share/Follow/Buy split is a great illustration: Follow being both trackable and undoable is exactly the kind of mix that a sealed class cannot express.
One trade-off worth naming alongside it: once subtypes can live in other files or modules, you lose the "one file, whole family" readability that makes a sealed class so easy to audit. I keep sealed classes for UI state (Loading/Success/Error, where the parent carries the data) and reach for sealed interfaces for actions and events, where the subtypes are capabilities rather than states.
Did you end up refactoring an existing UiState to a sealed interface, or did you keep the state as a sealed class and only move the actions over?