tezvyn:

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

Curated by the Tezvyn teamSource: kotlinlang.orgadvanced
When would you use a sealed class instead of an enum?

This tests closed hierarchies versus singleton constants. A strong answer contrasts enums with sealed class instances, shows a NetworkResult example with data-bearing branches, and highlights exhaustiveness.

WHAT THIS TESTS: This question evaluates whether you understand the difference between a set of singleton constants and a closed type hierarchy where each subtype can carry its own state. Interviewers want to see that you know sealed classes provide compile-time exhaustiveness with when expressions while allowing heterogeneous data shapes, something enums cannot do cleanly.

A GOOD ANSWER COVERS: First, the structural distinction: enum entries are singleton objects that all share the exact same constructor and properties, so every constant must carry the same fields even when they are irrelevant. Second, the sealed class advantage: each direct subclass can declare its own constructor parameters, so Success can hold a data payload and Error can hold a message and throwable while Loading needs nothing. Third, the when exhaustiveness guarantee: because all direct subclasses of a sealed class are known at compile time within the same module and package, a when expression used as a statement does not need an else branch; if you later add a new subclass, the compiler flags every incomplete when. Fourth, mention that sealed classes are abstract and cannot be instantiated directly, which enforces the hierarchy boundary.

COMMON WRONG ANSWERS: Saying that enums are fine because they can have abstract methods or properties misses the point that every enum constant still conforms to one uniform signature. Suggesting that data classes alone solve the problem ignores the closed hierarchy guarantee that prevents external code from creating new states that your when expressions do not handle. Another red flag is conflating sealed classes with sealed interfaces without explaining that both restrict the hierarchy but classes carry state more naturally for this use case.

LIKELY FOLLOW-UPS: The interviewer may ask how sealed classes interact with modules and visibility, specifically that no new subclasses can appear outside the module where the sealed class is defined. They might also ask how this compares to sealed interfaces in Java 15 or when to prefer a sealed interface over a sealed class. You could also be asked about serialization implications or how to handle state in ViewModels using sealed classes with Flow or LiveData.

ONE CONCRETE EXAMPLE: Imagine a network request represented by a sealed class called NetworkResult. Inside it, define object Loading to represent an in-flight request with no data. Define data class Success with a val data String to hold the response payload. Define data class Error with a val message String to hold the failure reason. In your UI rendering function, write a when statement over a NetworkResult parameter. In the Loading branch, show a progress indicator. In the Success branch, display the data string. In the Error branch, show the message string. Because NetworkResult is sealed, Kotlin verifies at compile time that these three branches cover every possibility. If you later add a Cancelled subclass inside the same package, every when expression using NetworkResult breaks the build until you handle the new state, which is exactly the safety sealed classes are designed to provide.

Source: kotlinlang.org

Read the original → kotlinlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

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