October 2019
Intermediate to advanced
434 pages
11h 54m
English
Sealed classes represent restricted type hierarchies. They are similar to enums, but each class in the hierarchy can have its own properties and methods.
In the following example, we've defined a sealed class named ViewState by using the sealed keyword:
sealed class ViewStateobject Loading : ViewState()data class Loaded(val article: Article) : ViewState()class Error(val error: Throwable?) : ViewState()
We've created three classes that extend ViewState. This class hierarchy may be defined within the class body or outside it. However, a sealed class may be subclassed only within the same file.
Each of these classes has its own unique properties. You may also notice that we've used object, data, and regular classes when defining ...
Read now
Unlock full access