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 - Kotlin Tutorial
From the course: Kotlin Essential Training: Object-Oriented and Async Code
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
-
-
-
Classes19m 32s
-
(Locked)
Interfaces12m 29s
-
(Locked)
Enum classes12m 13s
-
(Locked)
Data classes11m 47s
-
(Locked)
Object classes7m 9s
-
(Locked)
Sealed classes11m 42s
-
(Locked)
Companion objects6m 45s
-
(Locked)
Challenge: Create a Result type using a sealed class1m 45s
-
(Locked)
Solution: Create a Result type using a sealed class6m 13s
-
-
-
-