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.
This tests your understanding of Kotlin's type system for modeling restricted hierarchies, like network states. A strong answer differentiates them by data: `enum` constants are single instances of the same type, best for simple, fixed states. `Sealed class` is superior when states carry different data (e.g., `Success(data)` vs. `Error(exception)`), as its subclasses can be unique data classes or objects. A red flag is only mentioning `when` exhaustiveness, which both offer, and missing the core difference in state representation.
Read the original → kotlinlang.org
- #kotlin
- #android
- #sealed class
- #enum
- #architecture
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.