Kotlin

Kotlin Sealed Class

In Kotlin, a significant additional type of class is introduced that isn’t seen in Java. The new type is known as a sealed class in kotlin. The sealed class is from the built-in classes in kotlin. In the article, we will understand the concept of the sealed class in the kotlin language. The subclasses’ safety is provided via sealed classes, restricting the subclasses that can be matched at compile time rather than runtime.

A sealed class confines the hierarchy of classes. The “sealed” keyword can be used just before the class name to declare it as a sealed class. The compiler automatically detects the class is sealed when we have a class declaration sealed. It’s a symbol used to restrict the class hierarchy. A sealed class is used when an item has one of the classes from a specific set but can’t have a further class. The Constructors created of sealed classes are by default private and cannot be made non-private. It also throws an exception; if the class is inherited from the sealed class, we cannot Instantiate objects from the sealed class.

Syntax of the Sealed Class in Kotlin:

The general syntax of the sealed class we used in Kotlin is given below:

sealed class Class_name

fun main()

{

variable_name = sealed_className() //sealed types cannot be instantiated

}

First, we have to declare the sealed class name with the modifier “sealed,” and then, we have to define other subclasses in the sealed class. We have to do so because it is impossible to inherit the sealed class; we can use its reference to invoke the methods in the main function. Note that the seal class’s subclass should always be declared in the same file where the sealed class is located.

How to use the Sealed Class in Kotlin:

To understand the basics of using the sealed class in Kotlin language, we should take a look at the examples given below:

Example # 1: Creating the sealed class in Kotlin:

A sealed class may have subclasses, but they must all be defined in the same Kotlin file where the sealed class is present. We’ll see how to use a sealed class in the example below.

We have declared a sealed class with the modifier “sealed” in the above example code. The sealed class is named “myDemo.” The sealed class “myDemo” has two subclasses defined. The first subclass is named “One,” The second subclass is named “Two.” We have defined display functions in these subclasses, which have the kotlin println function. These println functions contain a string message shown in the code. Then, we have the main function for displaying the subclasses message. We have created an object of subclasses as “obj1” and “obj2”. The “obj1” is calling the “Two” subclass, and the “obj2” is calling the subclass “One.” These objects “obj1” and “obj2” are called with the display() function, which will display the content of the subclasses.

We have got the string message as output is shown in the below image.

Example # 2: Creating the sealed class with when expression in Kotlin:

When an expression is regularly used, sealed classes are commonly employed. Because sealed classes’ subclasses have their types, they behave as a case. As a result, the “when” expression is used in a sealed class covering all circumstances; there is no need to include an “else” clause.

We have the sealed class as “Animal” in the above code. The sealed class “Animal” is initialized with the string type property, defined in a variable “A1”. We have two subclasses defined as “Cat” and “Duck” in a sealed class. These subclasses contain string values passed in it. Another subclass, “Horse,” is defined outside the sealed class “Animal.” Then, we have a display() function that takes an object of type “Animal” and displays a corresponding message depending on the kind of class sealed; called “Animal.”

We have the “when” expression rather than the “else” clause. The “when” expression will cover all the cases, and we have passed a sealed class named to it. In the “when” expression, we have a keyword “is” that determines if the class belongs to one of the following types. The “is” keyword is only necessary for classes. Now, we have called the main function to create objects of given subclasses. In the end, these defined objects are called by display() function.

The “when” expression is used rather than the “else” clause, which is easier than the else clause. The output returns the sealed class using the “when” expression on the screen below.

Example # 3: Creating the sealed class with performing Arithmetic Operations in Kotlin:

We are performing four different types of arithmetic operations by using a sealed class with when expression in the below example.

We have declared a sealed class as “ArithmeticOperation” in the above example code. The sealed class ArithmeticOperation has defined four subclasses “Add,” “Multiply,” “subtract,” and “Divide.” We have defined these subclasses with the “Int” type. Then, we have defined a evaluate() function, which contains the “when” expression. The “is” keyword is used in the “when” expression, which will ensure that all subclasses from the specified subclass set are referenced. Then, we have the main function, which is defined with the variables “x” and “y” with the numeric value “5”. We have also defined other variables for subclasses in which specified arithmetic operations are initialized.

Upon executing the different arithmetic operations, we have the output values on the terminal screen of ubuntu.

Conclusion:

The article’s main aim is to demonstrate to you the concept of the sealed class, which is introduced in the kotlin language. The sealed classes in kotlin allow us to write code that is both clear and concise. For your better understanding, we have different examples of using sealed classes, and also we have learned the usage of “when” expressions. That’s all about the sealed classes you need to know in Kotlin and how to utilize them in the code.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.