Kotlin Sealed Class

In this tutorial you will learn about the Kotlin Sealed Class and its application with practical example.

Kotlin Sealed Class

The class which is declared with “sealed” modifier before the name of class known as sealed class. Sealed classes are used to represent restricted class hierarchies. A sealed class can have subclasses but all of them need to declare in the same file as the sealed class itself. In Kotlin sealed class we don’t need to use “else” statement.

Table Of Contents

Example: Let’s get understand it by an example, see below code.

Here, SealedDemo is a “sealed” class, which has two types: OPT1 & OPT2.
In the main class have created an object and assigned at run time.
Here we have applied “when” clause so that we can implement the final output at runtime.

Output: The output of this code will look like below image.

sealed-1

In this tutorial we have learn about the Kotlin Sealed Class and its application with practical example. I hope you will like this tutorial.