Menu

I Was Using Sealed Classes Wrong in Android for Two Years. Here’s What Nobody Told Me.
📰

I Was Using Sealed Classes Wrong in Android for Two Years. Here’s What Nobody Told Me.

DEV Community: mobile·supriya shah·9 days ago
#7OKZLwA6
#devtomobile#devto#dev#sealed#class
Long article (1301 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.