When would you use a sealed class instead of an enum?

This tests your understanding of state representation. Explain that sealed classes define a hierarchy of types that can hold different data, while enums are a set of singleton constants.
What's really being asked
This question tests your understanding of a core concept in type-safe design: modeling restricted hierarchies. The interviewer isn't just checking syntax; they want to see if you can differentiate between a fixed set of constants (enums) and a fixed set of types that can each carry their own unique state (sealed classes). Your ability to articulate this difference, especially in the context of state management (like API responses or UI states), demonstrates senior-level language proficiency.
The full answer
First, state the core difference: enums represent a set of singleton instances, while sealed classes represent a closed hierarchy of types. All enum constants are of the same type. Subclasses of a sealed class can be different types.
Second, explain the key advantage of sealed classes: holding state. Because subclasses are distinct types, they can have their own properties. For example, Success(data: User) holds a User object, while Error(message: String) holds an error message. An enum cannot elegantly model this heterogeneous state.
Third, mention the similarity: both work with when expressions to provide compile-time exhaustiveness. The compiler will force you to handle every possible case, which prevents runtime errors when new states are added.
Finally, provide the requested code example, showing an object for a stateless case (Loading) and classes for stateful cases (Success, Error).
The mistakes people make
A major red flag is suggesting they are mostly interchangeable. Another is trying to shoehorn state into an enum, for example, by adding a mutable var data: Any? property. This is not type-safe and defeats the purpose of using a restricted class system. A less severe mistake is only describing what they are without explaining why you'd choose one over the other based on the need to associate different data with different states.
What usually comes next
Expect follow-ups like, "When would you use a sealed interface instead of a sealed class?" (Answer: For multiple inheritance, or when classes in the hierarchy don't share a common implementation). Another is, "Can a subclass of a sealed class be another sealed class?" (Answer: Yes, to create more complex, nested state machines).
A concrete example
To model a network request, you could define a sealed class like this:
sealed class NetworkResult {
object Loading : NetworkResult()
data class Success(val data: List<String>) : NetworkResult()
data class Error(val message: String) : NetworkResult()
}
Then, you can use an exhaustive when expression to handle all possible states in a type-safe way:
fun handleResult(result: NetworkResult) {
when (result) {is NetworkResult.Loading -> println("Loading...")
is NetworkResult.Success -> println("Success: ${result.data.size} items loaded")
is NetworkResult.Error -> println("Error: ${result.message}")
}
}
If you later added a NetworkResult.Cancelled state, the compiler would show an error at every when block until you handled the new case.
Interview question
To model a network request's state—`Loading`, `Success` (with data), or `Error` (with a message)—which Kotlin construct is most suitable?
- a.An enum, because it represents a fixed set of constants, which is what these states are.
- b.A sealed interface, because it provides maximum flexibility if the states need to inherit from other classes.
- c.An enum with nullable properties for both data and an error message to accommodate all states.
- d.A sealed class, because each of its subclasses can carry different data types specific to that state.Correct
Why? this is the answer
A sealed class is ideal because its subclasses can represent states with different associated data (e.g., data for Success, a message for Error) in a type-safe way. An enum is a set of constants of the same type and cannot elegantly model states with different data requirements.
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