From the course: Kotlin Essential Training: Object-Oriented and Async Code

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Sealed classes

Sealed classes

- [Instructor] Much like enum classes, sealed classes allow us to define restricted class hierarchies. However, unlike enum classes each subtype in the hierarchy may include unique properties making them more flexible and perfect for modeling things like uistates or operation results. So we're going to create our first sealed class here in IntelliJ by going to source, main, Kotlin, right clicking, navigating to new Kotlin class or file. And then from this option, we'll select sealed class and we are going to name that sealed class uistate and then hit enter. So we will imagine that we're going to use this uistate sealed class to represent uistates in let's say a mobile application or web application. If this was an enum, each type in the enum would have to have the same properties but that doesn't work well for complex states. For example, in our uistate here we might want to have a loading state and that loading state…

Contents