When Sealed Class Kotlin

How To Articles

In Kotlin, the sealed keyword is used to define a sealed class. A sealed class is a special kind of abstract class that can be subclassed from within the same file where it is defined. It restricts the inheritance hierarchy by limiting the classes that can inherit from it.

When I first started learning Kotlin, the concept of sealed classes was a bit confusing to me. However, as I delved deeper into the language, I discovered the power and flexibility that sealed classes provide. Let me explain in detail how and when to use sealed classes in Kotlin.

What is a sealed class?

A sealed class is declared using the sealed keyword followed by the name of the class. Unlike abstract classes, you cannot create instances of a sealed class directly. It serves as a base class for a limited set of derived classes.

For example, let’s say we are developing an application that models different types of animals. We can define a sealed class called Animal and have specific animal types like Cat, Dog, and Horse extend the sealed class.

sealed class Animal

class Cat : Animal()
class Dog : Animal()
class Horse : Animal()

Advantages of sealed classes

One of the advantages of using sealed classes is that it allows you to define a closed set of classes. This means that all possible subclasses of a sealed class must be defined in the same file where the sealed class is defined. This ensures that the inheritance hierarchy is known and controlled within a specific scope.

In addition, sealed classes are often used in conjunction with when expressions, providing a powerful pattern matching mechanism in Kotlin. When combined with sealed classes, when expressions provide exhaustive checks, ensuring that all possible subclasses are handled.

How to use sealed classes

When working with sealed classes, it’s common to use when expressions to perform different actions based on the type of the object. The when expression acts as a switch statement on steroids, allowing you to handle different cases based on the type of the sealed class.

For example, in our animal example, we can use a when expression to perform specific actions for each type of animal:

fun doAction(animal: Animal) {
    when (animal) {
        is Cat -> {
            // perform actions specific to cats
        }
        is Dog -> {
            // perform actions specific to dogs
        }
        is Horse -> {
            // perform actions specific to horses
        }
    }
}

By using a when expression, we can easily handle different types of animals without the need for repetitive if statements or type casting.

Conclusion

Sealed classes in Kotlin provide a powerful mechanism for defining a closed set of classes and ensuring exhaustive checks when working with subclasses. They offer flexibility and control in the inheritance hierarchy of your code.

As I dove deeper into Kotlin and started using sealed classes in my projects, I quickly realized their benefits and how they simplify my code. They allow me to handle different cases in a clean and efficient way, making my code more readable and maintainable.

I hope this article has given you a deeper understanding of sealed classes and how to use them in Kotlin. Happy coding!