in , ,

Kotlin Sealed Classes

Kotlin Sealed Classes
Kotlin Sealed Classes

Kotlin Sealed Class: In this tutorial, you will learn about Sealed class, how they are created, and when to use them with the assistance of examples.

Sealed classes are used when a worth can have just one of the types from a limited set (restricted hierarchies).

A sealed class is used for addressing a restricted class hierarchy where an object or a worth can have one of the sorts from a limited set of values. You can consider a sealed class an extension of an enum class. The set of values in enum class is likewise restricted, anyway an enum constant can have just single occurrence while a subclass of a sealed class can have different cases.


Prior to going into details about sealed classes, let’s explore what problem they solve. Let’s take an example (taken from the official Kotlin website – Sealed classes article):

class Expr
class Const(val value: Int) : Expr
class Sum(val left: Expr, val right: Expr) : Expr

fun eval(e: Expr): Int =
        when (e) {
            is Const -> e.value
            is Sum -> eval(e.right) + eval(e.left)
            else ->
                throw IllegalArgumentException("Unknown expression")
        }

In the above program, the base class Expr has two derived classes Const (addresses a number) and Sum (addresses sum of two expressions). Here, it’s compulsory to use else branch for default condition in when expression.

Presently, on the off chance that you derive another subclass from the Expr class, the compiler will not distinguish anything as else branch handles it which can prompt bugs. It would have been better if the compiler issued a blunder when we added a new subclass.

To take care of this issue, you can use a sealed class. As referenced, a sealed class restricts the chance of creating subclasses. Also, when you handle all subclasses of a sealed class in a when expression, it’s not important to use else branch.


To create a sealed class, sealed modifier is used. For instance,

sealed class Expr

Example: Kotlin Sealed Class

Here’s the means by which you can take care of the above issue using sealed class:

sealed class Expr
class Const(val value: Int) : Expr()
class Sum(val left: Expr, val right: Expr) : Expr()
object NotANumber : Expr()


fun eval(e: Expr): Int =
        when (e) {
            is Const -> e.value
            is Sum -> eval(e.right) + eval(e.left)
            NotANumber -> java.lang.Double.NaN
        }

As should be obvious, there is no else branch. On the off chance that you derive a new subclass from Expr class, the compiler will complain unless the subclass is handled in the when expression.


Few Important Notes

  • All subclasses of a sealed class should be declared in a similar file where the sealed class is declared.
  • A sealed class is abstract without anyone else, and you can’t start up objects from it.
  • You can’t create non-private constructors of a sealed class; their constructors are private by default.

Difference Between Enum and Sealed Class

Enum class and sealed class are pretty comparable. The set of values for an enum type is likewise restricted like a sealed class.

The only difference is that, enum can have recently a single case, while a subclass of a sealed class can have various instances.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

HTML Introduction

HTML Introduction

HTML Editors

HTML Editors