Tutorial

Kotlin Sealed Class

Published on August 4, 2022
author

Anupam Chugh

Kotlin Sealed Class

In this tutorial, we’ll be looking into Kotlin Sealed Class. What are they? What’s their use? We’ll be addressing all of these things below.

Kotlin Sealed Class

kotlin sealed class In layman terms, as the name suggests, sealed classes are sealed or closed, hence making them restricted. Sealed classes are used for representing restricted class hierarchies wherein the object or the value can have value only among one of the types, thus fixing your type hierarchies. Sealed classes are commonly used in cases, where you know what a given value to be only among a given set of options.

Implementing Kotlin Sealed Classes

Sealed classes in Kotlin are implemented in the following manner.

sealed class A{
    class B : A()
    class C : A()
}

To specify a sealed class, you need to add the modifier sealed. A sealed class cannot be instantiated. Hence, are implicitly abstract. The following WON’T work.

fun main(args: Array<String>) 
{
    var a = A() //compiler error. Class A cannot be instantiated.
}

Constructors of a sealed class are private by default. All subclasses of a sealed class must be declared within the same file. Sealed classes are important in ensuring type safety by restricting the set of types at compile-time only.

sealed class A{
    class B : A() 
    {
        class E : A() //this works.
    }
    class C : A()

    init {
        println("sealed class A")
    }

}

class D : A() //this works
{
class F: A() //This won't work. Since sealed class is defined in another scope.
}

Creating a sealed class with constructors.

sealed class A(var name: String){
    class B : A("B")
    class C : A("C")
}

class D : A("D")
fun main(args: Array<String>) {
    
    var b = A.B()
    var d = D()
}

Adding Data Class and Object in a sealed class.

fun main(args: Array<String>) {

    val e = A.E("Anupam")
    println(e) //prints E(name=Anupam)

    var d = A.D
    d.name() //prints Object D
}


sealed class A{
    class B : A()
    class C : A()
    object D : A()
    {
         fun name()
         {
             println("Object D")
         }
    }
    data class E(var name: String) : A()

}

Difference between enum and sealed classes

In Kotlin, Sealed Classes can be termed as Enum classes on steroids. Sealed classes allow us to create instances with different types, unlike Enums which restrict us to use the same type for all enum constants. The following isn’t possible in Enum classes.

enum class Months(string: String){
January("Jan"), February(2),
}

Enum classes allow only a single type for all constants. Here’s where sealed classes come to our rescue by allowing multiple instances.

sealed class Months {
    class January(var shortHand: String) : Months()
    class February(var number: Int) : Months()
    class March(var shortHand: String, var number: Int) : Months()
}

How can you use this feature of Sealed classes in your Projects? In a newsfeed like application, you can create three different class types for Status, Image, Video posts as shown below.

sealed class Post
{
    class Status(var text: String) : Post()
    class Image(var url: String, var caption: String) : Post()
    class Video(var url: String, var timeDuration: Int, var encoding: String): Post()
}

This isn’t possible with Enum classes.

Sealed classes and when

Sealed classes are commonly used with when statements since each of the subclasses and their types act as a case. Moreover, we know that the Sealed class limits the types. Hence, the else part of the when statement can be easily removed. Following example demonstrates the same.

sealed class Shape{
    class Circle(var radius: Float): Shape()
    class Square(var length: Int): Shape()
    class Rectangle(var length: Int, var breadth: Int): Shape()
}

fun eval(e: Shape) =
        when (e) {
            is Shape.Circle -> println("Circle area is ${3.14*e.radius*e.radius}")
            is Shape.Square -> println("Square area is ${e.length*e.length}")
            is Shape.Rectangle -> println("Rectagle area is ${e.length*e.breadth}")
        }

Let’s execute the eval function in our main function as shown below.

fun main(args: Array<String>) {

    var circle = Shape.Circle(4.5f)
    var square = Shape.Square(4)
    var rectangle = Shape.Rectangle(4,5)

    eval(circle)
    eval(square)
    eval(rectangle)
    //eval(x) //compile-time error.

}

//Following is printed on the console:
//Circle area is 63.585
//Square area is 16
//Rectangle area is 20

Note: The is modifier checks if the class is of the following type. is modifier is required only for classes. Not with Kotlin objects as shown below:

sealed class Shape{
    class Circle(var radius: Float): Shape()
    class Square(var length: Int): Shape()
    object Rectangle: Shape()
    {
        var length: Int = 0
        var breadth : Int = 0
    }
}

fun eval(e: Shape) =
        when (e) {
            is Shape.Circle -> println("Circle area is ${3.14*e.radius*e.radius}")
            is Shape.Square -> println("Square area is ${e.length*e.length}")
            Shape.Rectangle -> println("Rectangle area is ${Shape.Rectangle.length*Shape.Rectangle.breadth}")
        }

This brings an end to kotlin sealed class tutorial. References: Kotlin Docs

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Anupam Chugh

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 31, 2020

Best tutorial in wild web for kotlin (;

- Meraj

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 14, 2020

    What is the Sealed class equivalent in Java? @Functional Interface?

    - Ayman

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      May 6, 2020

      well written, really helpful

      - Pooja Gurav

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        December 5, 2019

        Thank you!

        - Sajjad

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Become a contributor for community

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          DigitalOcean Documentation

          Full documentation for every DigitalOcean product.

          Resources for startups and SMBs

          The Wave has everything you need to know about building a business, from raising funding to marketing your product.

          Get our newsletter

          Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

          New accounts only. By submitting your email you agree to our Privacy Policy

          The developer cloud

          Scale up as you grow — whether you're running one virtual machine or ten thousand.

          Get started for free

          Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

          *This promotional offer applies to new accounts only.