Why Kotlin Sealed Class exists?

Why Kotlin Sealed Class exists?

Introduction

Kotlin, being an emerging open-source programming language provides lesser boilerplate and also some additional features which help manage the code flow better.

A significant addition to kotlin is Sealed Class which was not seen in Java. In simple words, it actually restricts the hierarchy of the normal class. Let's discuss the Sealed class in detail:

Syntax

sealed modifier is used to declare a sealed class

sealed class Class{
  class Class1:Class(){}
  class Class2:Class(){}
  ... (fixed number of classes)
}

From the above snippet, we can see that the sealed class holds many other classes which are returning the parent class(sealed class) object. Moreover, the number of classes should be fixed to implement the sealed class.

Practically we use these types of classes in the case when we know how many Values a class can return.

As the sealed classes are abstract, they cannot be instantiated.

Why and when do we use sealed class

Still, the question is when and why we should prefer sealed classes. When just a small number of options are available for a value and each of these options differs in functionality, we can consider a sealed class. Kotlin's sealed class is one of the built-in classes that can be used to prevent users from inheriting a class by any chance.

How sealed classes are different from ENUMS

To get a better understanding I will explain these ENUMS as something whose values can be restricted but sealed classes are those whose type can be restricted.
Lets us understand this through an example: Consider a developer game where we have 6 cards. Each card has a unique colour and the user needs to decide the colour according to the hex code given. In this situation, we use ENUMS, as the value is colour which cannot b repeated. But let me change this situation to 6 cards where some cards may have the same colour, but each card has a different image in it. This is where we use sealed classes which are TYPE restricted.

Creating a sealed class

sealed class Name{
   class FistName:Name(){
       fun print(){
           println("Hello, I am first name")
       }
    }

   class LastName:Name(){
       fun print(){
           println("Hello, I am last name")
       }
    }

}

fun main(){
  val obj1 = Name.FirstName()
  obj1.print() //Hello, I am first name
  val obj2 = Name.LastName()
  obj2.print() //Hello, I am last name

Important Points of the Sealed Class

  • A sealed class's subclasses must all be specified in the same file as the sealed class itself.

  • Sealed classes are abstract and can have abstract members.

  • Public constructors cannot be used in sealed classes.

  • Sealed classes cannot be instantiated directly.

In android development, this class is used commonly to implement the Retrofit response as a Result sealed class as you know network calls have a fixed number of results: success, failure and loading.

Conclusion

In this article, we learned some basics of the sealed class, its syntax, its usage and also its difference from ENUMS. For more reference use this.

Hope you got a gist of the sealed class and that you are already excited to implement it in your next project ๐Ÿš€

Did you find this article valuable?

Support Subhadip Das by becoming a sponsor. Any amount is appreciated!

ย