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 - 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
-
-
-
(Locked)
Communicate with Snackbar messages6m 15s
-
(Locked)
Manage view components with Butter Knife6m 41s
-
Create a reusable dialog class9m 22s
-
(Locked)
Convert a layout to ConstraintLayout7m 13s
-
(Locked)
Manage global data with singletons5m 37s
-
(Locked)
Convert image files to WEBP format6m 5s
-
(Locked)
Create an Espresso user interface test5m 18s
-
(Locked)
Create classes for Android with Kotlin5m 12s
-
(Locked)
Run background tasks in intent services8m 51s
-
(Locked)
Send broadcast messages with EventBus8m 44s
-
Handle events with Java 8 syntax5m 51s
-
(Locked)
Improve forms with design layouts7m 55s
-
(Locked)
Display draggable rows in a list of data8m 1s
-
(Locked)
Manage persistent data with Realm10m 23s
-
(Locked)
Play an audio file from assets8m 56s
-
(Locked)
Send an SMS message from an app7m 56s
-
(Locked)
Get a list of audio files on a device8m 32s
-
(Locked)
Play sounds with SoundPool8m 45s
-
(Locked)
Display the build date of an APK file6m 47s
-
(Locked)
Build Kotlin classes with properties7m 32s
-
(Locked)
Manage versioning with Gradle variables6m 8s
-
(Locked)
Use reporting tools from the Google Play Console4m 44s
-
Make web service calls with Retrofit10m 39s
-
(Locked)
Make asynchronous calls with Retrofit4m 6s
-
(Locked)
Send request parameters with Retrofit4m
-
(Locked)
Upgrade a project to Android Studio 3.04m 47s
-
(Locked)
Clean up calls to findViewById()2m 58s
-
(Locked)
Create adaptive launcher icons5m 47s
-
(Locked)
Use XML and downloadable fonts6m 40s
-
(Locked)
Explore a device's file system4m 55s
-
Profile an app's memory usage3m 52s
-
(Locked)
Manage an SQLite database with Room9m 17s
-
(Locked)
Manage background threads with Executors5m 32s
-
(Locked)
Track vital stats in a published app3m 4s
-
(Locked)
Inspect database files with DB Browser4m 41s
-
(Locked)
Execute tasks with JobScheduler7m 29s
-
(Locked)
Create Room entity classes in Kotlin8m 22s
-
(Locked)
Create Room DAO classes in Kotlin4m 13s
-
(Locked)
Upgrade to Android Studio 3.13m 22s
-
(Locked)
Convert a project to use Kotlin3m 20s
-
(Locked)
Handle events with Kotlin lambdas6m 13s
-
Manage menus with when3m 44s
-
(Locked)
Define global constants with Kotlin6m 12s
-
(Locked)
Use Kotlin data and enum classes5m 24s
-
(Locked)
Create inheritance hierarchies in Kotlin6m 8s
-
(Locked)
Manage data collections in Kotlin5m 45s
-
(Locked)
Sort data collections with Kotlin5m 33s
-
Filter data collections with Kotlin3m 52s
-
(Locked)
Create Kotlin extension functions2m 31s
-
Use support functions from Android KTX4m 30s
-
(Locked)
Use virtual devices with Google Play3m 50s
-
(Locked)