From the course: Android Development Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Create inheritance hierarchies in Kotlin

Create inheritance hierarchies in Kotlin - Android Tutorial

From the course: Android Development Tips

Create inheritance hierarchies in Kotlin

- [Instructor] When you want to represent a hierarchy of types, with super and subclasses, you can use Kotlin's sealed class architecture. A sealed class is designed to be used as a superclass. All of the class's subclasses must be declared in the same file as the superclass, either as nested classes or as independent classes. I'll demonstrate this using this version of my clothing item class, which I first created in last week's tip. This time, my enum for clothing types only has two possible values: shirt and pants. And in my main activity, I'm creating a clothing item with a type of shirt, by passing in that value from the enumeration. I'm going to change this from a data class to a sealed class. Now the first effect this has is to make the class abstract. It can't be instantiated directly. And if I go back to main activity, I'll now see that there's an error. It's telling me that the constructor is private. It can't be called from another context. Now go back to the clothing item…

Contents