Kotlin Class Types Series – Part 8: Sealed Classes
Rahul Pahuja
Staff Software Engineer @ CyberArk | Expertise in Android , iOS Development | MVC | MVVM | Java | Kotlin | Swift | Unit Testing | Automation Testing using Python and Appium
Kotlin’s sealed classes are one of the most powerful tools for managing state and enforcing strict type hierarchies. They act as a hybrid between enum and abstract classes, allowing controlled subclassing while preventing unrestricted inheritance.
If you’re writing Android or iOS apps, sealed classes can simplify state handling, make your code more readable, and reduce error-prone conditionals. Let’s dive in! ??
?? What Are Sealed Classes?
A sealed class restricts inheritance to a fixed set of subclasses defined in the same file. This makes them great for modeling finite state hierarchies.
sealed class NetworkResult {
data class Success(val data: String) : NetworkResult()
data class Error(val message: String) : NetworkResult()
object Loading : NetworkResult()
}
?? Why Not Just Use Enums?
Unlike enum class, sealed classes allow each state to hold different types of data, making them more flexible. Enums, on the other hand, only store constant values and shared properties.
? Performance Considerations
? Avoids Exhaustive When Checks at Runtime – The compiler forces you to handle all cases in when, reducing runtime errors.
? No Overhead of Reflection – Unlike annotations or reflection-based solutions, sealed classes are lightweight and optimized.
?? Pitfall: Not Inline-Friendly – If used extensively in loops or hot paths, sealed class hierarchies can introduce small memory overhead.
?? Do: Use them for modeling UI state, API responses, or event-based architectures. ?? Don't: Use them for frequently changing data structures (e.g., large JSON parsing models).
?? Testing Sealed Classes
Sealed classes improve testability by making state management explicit. Here’s how you can test them:
@Test
fun `network success returns expected data`() {
val result: NetworkResult = NetworkResult.Success("Hello, Kotlin!")
assertTrue(result is NetworkResult.Success)
assertEquals("Hello, Kotlin!", (result as NetworkResult.Success).data)
}
?? Pitfall: Serialization Issues – JSON parsers like Moshi or Gson don’t support sealed classes out of the box. You’ll need custom adapters.
?? Maintainability & Best Practices
? When to Use Sealed Classes
? When to Avoid Sealed Classes
?? Dos & Don’ts
? Do: Use them for strictly defined, predictable states. ? Do: Leverage them in when expressions for exhaustive pattern matching. ? Don't: Use them as replacements for simple enums. ? Don't: Overuse them for cases where traditional polymorphism suffices.
?? Final Thoughts
Sealed classes are a game-changer for structured state management in Kotlin. They help enforce strict type hierarchies, reduce boilerplate, and enhance code maintainability. Whether you're a junior Android engineer or a staff iOS developer, mastering sealed classes will improve your code quality significantly.
Next up in our 12-part Kotlin Class Series: Part 9 – Abstract Classes! Stay tuned! ??
Have you used sealed classes in your projects? Drop your thoughts below! ??
#Kotlin #AndroidDev #iOSDev #SoftwareEngineering #SealedClasses