Introduction to Sealed Classes and Interfaces
Explore how to use sealed classes and interfaces in Kotlin to build restricted and controlled type hierarchies. This lesson helps you understand their rules, differences, and advantages for safer polymorphism and better code organization in Kotlin applications.
We'll cover the following...
The Result interface and abstract class
Classes and interfaces in Kotlin are not only used to represent a set of operations or data. We can also use classes and inheritance to represent hierarchies through polymorphism. For instance, let’s say that we send a network request. As a result, we either successfully receive the requested data, or the request fails with some information about what went wrong. These two outcomes can be represented using two classes that implement an interface:
Alternatively, we could use an abstract class:
Restricted hierarchies
With either of these, we know that when a function returns ...