Skip to content

About sealed class in android

Devrath edited this page Dec 22, 2023 · 15 revisions

Sealed classes are abstract classes

There are only abstract methods defined and concrete implementations are defined.

sealed class Animal {
    object Cat : Animal() // We can define a Object classes like this 
    data class Dog(val name: String) : Animal() // We can define data classes like this
}

All the members of the sealed class must be defined in the same file

  • Here you can observe that we are inheriting Animal for Camel and Monkey but it's defined in the same file. If we try to inherit in another file, it will not work.
  • What the sealed modifier does is, that it is impossible to define another subclass outside the file.
  • Because of this, we know the sub-classes of a class by just analyzing a single file.
  • In Kotlin, a sealed class is typically defined in a single file. However, you can extend a sealed class with subclasses in different files. Each subclass should be in its own file, and they can be spread across multiple files within the same package.

Animal.kt

package com.demo.subclasses

sealed class Animal {
    object Cat : Animal() // We can define a Object classes like this
    data class Dog(val name: String) : Animal() // We can define data classes like this
}
object Camel : Animal()
object Monkey : Animal()

A bit of understanding on how classes are different in kotlin compared to java

  • In kotlin classes are final by default meaning they cannot be inherited by just extending.
  • We need to explicitly make them open to be inherited.
  • Sealed classes fit between final and open. This Final <-------> Sealed classes <-------> Open
  • So sealed classes help in taking advantage of inheritance but in a limited format.
  • This will ensure we don't end up with a massive long inheritance tree and not knowing easily where it's inherited by whom but here all the children are in the same class.

A sealed class can be extended by a normal class

  • Earlier we noticed that, If a sealed class is inheriting another sealed class, it has to be defined in the same file to restrict the hierarchy.
  • But say a normal class, It can extend the sealed class even outside the file of sealed class

Monkey.kt

class Monkey : Animal() {
     init {
         println("Initializing the monkey class")
     }
}

Member functions and member variables marked private

  • If we mark the member-function and member-variable private, no one outside the sealed class can access it.
  • No instance of sealed class also can access it if marked private.

Can we define constants in sealed class

  • We can do it by making companion object and defining the constants inside.
  • We cannot define the constants outside the companion object.
sealed class Animal {
    companion object {
        const val color : String = "Red"
        const val age : Int =  50
    }
    //const val height : Int =  50 // -----------> This is not possible
}

Can we create an object of sealed class

  • We cannot create a object of sealed class
  • But we can extend normal class with sealed class and create a object of normal class.
Clone this wiki locally