-
Book Overview & Buying
-
Table Of Contents
Kotlin for Java Developers
By :
Unlike many, this concept is very easy to explain since we can summarize it in very few words, and it is easy to understand. Sealed classes are used to represent restricted class hierarchies.
If we want to give a more detailed explanation, we can say that sealed classes allow us to represent limited data types through a restricted hierarchy of classes. We can also use them to represent states.
We can explain sealed classes in two ways for Java developers. For developers using Java versions prior to Java 15, the concept might seem somewhat similar to enums, as both can represent a restricted set of types. However, they are fundamentally different: enums are singleton objects that represent a single state, while sealed classes can define hierarchies of objects or classes. In Kotlin, we also have enums, and we will cover them later. In this chapter, we will not compare the concepts. For developers who have worked with Java version 15 or higher, it is possible that...