Originally published on Medium:
https://medium.com/@supsabhi/i-was-using-sealed-classes-wrong-in-android-for-two-years-heres-what-nobody-told-me-3daa5cbdcbca?sharedUserId=supsabhi
A beginner-friendly story about one of Kotlin’s most misunderstood features — and why the distinction finally matters.
We’ve all been there…Okay, real talk.
When I first started Android development, while developing a sleek new feature to handle different states, I thought, “Okay, I’ll just use an Enum.” But then, reality hits. Success state needs to carry a User object, while Error state needs an Exception. Enums start sweating. They can’t do that.
And then I came across the term sealed class and thought, "Cool. It's like an enum, but fancier." I used it everywhere. State management? Sealed class. Network results? Sealed class. UI events? Sealed class.
Then Kotlin 1.5 quietly introduced sealed interfaces and I thought, “Why would I ever need that? I already have sealed classes.”
I was wrong.…