When to use a sealed class instead of an enum?

Tests your grasp of Kotlin's type hierarchies. Use sealed class for states with associated data (e.g., Success(data)), as subclasses can have unique properties. Use enum for simple, constant states. A red flag is treating them as interchangeable.
What's really being asked
This question probes your ability to model complex, yet finite, states in a type-safe way. The interviewer wants to see if you understand the fundamental difference between a set of constant values (enum) and a restricted class hierarchy (sealed class). It's a test of choosing the right tool for state representation, particularly when states can carry different associated data.
The full answer
A strong answer explains the core distinction and then provides a concrete example. First, clarify the limitation of enums: all enum constants are instances of the same enum class and cannot hold different types of data. They are perfect for simple, fixed sets of values like Direction.NORTH or Status.PENDING. Second, introduce sealed classes as the solution for richer state machines. Explain that a sealed class allows you to define a restricted hierarchy where subclasses can be completely different types (data class, object, regular class). This is the key. Third, provide the canonical network request example: sealed class NetworkResult. Define subclasses like object Loading, data class Success(val data: User), and data class Error(val message: String). This shows how each state can hold different or no data. Fourth, demonstrate how the when expression provides compile-time exhaustive checks, preventing you from forgetting to handle a state.
The mistakes people make
A major red flag is saying they are mostly interchangeable or that the only difference is syntax. Another weak answer focuses only on the when statement's exhaustiveness, which is a benefit of both enums and sealed classes. The crucial difference is the ability of sealed class subclasses to have their own unique properties and state. Forgetting to mention that subclasses can be a data class, object, or class shows a shallow understanding. Saying a sealed class can have infinite subclasses is also incorrect; the hierarchy is restricted to the file or module where it's defined.
What usually comes next
"What's the difference between a sealed class and a regular abstract class with a private constructor?" (Answer: The compiler knows all direct subclasses of a sealed class at compile time, enabling exhaustive when checks, which isn't true for a regular abstract class). "Can a sealed class have multiple instances of a subclass?" (Answer: Yes, if the subclass is a data class or class, like Success(user1) and Success(user2). If it's an object, it's a singleton).
A concrete example
For representing UI state, an enum is too simple. enum class UiState { LOADING, SUCCESS, ERROR } can't hold the user data on success or the error message. A sealed class is perfect: sealed class UiState { object Loading : UiState(); data class Success(val users: List<User>) : UiState(); data class Error(val message: String) : UiState(); }. Now your Success state can actually hold the list of users, which is impossible with an enum.
Interview question
To model a state machine where each state might carry unique, type-specific data (e.g., a success state holding a user object, an error state holding a message), which Kotlin construct is most appropriate?
- a.An enum class
- b.A sealed classCorrect
- c.An interface
- d.An abstract class with a private constructor
Why? this is the answer
Sealed classes are specifically designed for representing restricted hierarchies where subclasses can be distinct types (like data classes or objects) and hold different associated data. Enum classes cannot hold different types of data for each constant, making them unsuitable for states with unique payloads.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
- #kotlin
- #android
- #sealed class
- #enum
- #architecture
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles