Open In App

Kotlin Sealed Classes

Last Updated : 10 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Kotlin introduces a powerful concept that doesn't exist in Java: sealed classes. In Kotlin, sealed classes are used when you know in advance that a value can only have one of a limited set of types. They let you create a restricted class hierarchy, meaning all the possible subclasses are known at compile-time.

The word "sealed" refers to something that is closed or restricted.

This helps make your code safer and easier to manage, especially when you're using conditions like when expressions. Because the compiler already knows all possible subclasses of a sealed class, you don’t need to use an else case in a when block, which makes your logic more predictable.

To create a sealed class, you simply add the sealed keyword before the class declaration:

sealed class Demo

You cannot create an object of a sealed class directly because it is implicitly abstract. That means you cannot do this:

fun main() {
// This will cause a compiler error val d = Demo() }

That's because sealed classes are designed to be extended - not instantiated on their own. Also, sealed class constructors are protected by default, so you can’t access them outside the class or its subclasses.

Defining Subclasses

All the subclasses of a sealed class must be defined in the same Kotlin file. They don’t have to be inside the sealed class, but they must be in the same file where the sealed class is defined.

Example:

Kotlin
sealed class Demo {
    class A:Demo(){
        fun display(){
            println("Subclass A of Sealed class Demo ")
        }
    }
    class B:Demo(){
        fun display(){
            println("Subclass B of sealed class Demo")
        }
    }
}

fun main(args: Array<String>){
    val obj =Demo.B()
    obj.display()
    val obj1=Demo.A()
    obj1.display()
}


Output:

Subclass B of sealed class Demo
Subclass A of sealed class Demo

Note: All the subclasses of the sealed class must be defined within the same Kotlin file. However, it not necessary to define them within the sealed class, they can be defined in any scope where the sealed class is visible.

Example:

Kotlin
// A sealed class with a single subclass defined inside
sealed class ABC {
 class X: ABC(){...}
}

// Another subclass of the sealed class defined
class Y: ABC() {
  class Z: ABC()   // This will cause an error. Sealed class is not visible here
}


Using Sealed Classes with when

The most common and useful way to use sealed classes is with Kotlin’s when expression. Since the compiler knows all the possible types, it checks if you've handled every case. That way, you don't need a default else block, which makes your code cleaner and safer.

Example:

Kotlin
sealed class Fruit

class Apple : Fruit()
class Mango : Fruit()
class Pomegranate : Fruit()

fun describe(fruit: Fruit) {
    when (fruit) {
        is Apple -> println("Apple is good for iron")
        is Mango -> println("Mango is delicious")
        is Pomegranate -> println("Pomegranate is good for vitamin D")
    }
}

fun main() {
    describe(Apple())
    describe(Mango())
    describe(Pomegranate())
}


Output:

Apple is good for iron
Mango is delicious
Pomegranate is good for vitamin d

In this example, all three subclasses of Fruit are known to the compiler, so there's no need for an else branch in the when statement. If you forget to handle any subclass, the compiler will show an error, which is great for catching bugs early.

Advantages of Sealed Classes in Kotlin

  1. The compiler knows all possible types, so you’ll get errors if you forget to handle any case. This prevents bugs early.
  2. Since all types are known, you can list them one by one in a when block without needing an else.
  3. All subclasses must be in the same file, so it's easy to see and control all possible types in one place.
  4. Use sealed classes when something has limited outcomes — like Success, Error, or Loading.
  5. Each sealed class type can have its own data and functions, unlike enums which are more basic.
  6. Using sealed classes with when makes your code shorter and easier to read than long if-else chains.

Article Tags :

Similar Reads