Why we need sealed class in kotlin android because in kotlin all classes are final by default

108 views Asked by At

the below sealed class restricts the class hierarchies

sealed class Fruit(val x : String)
{
    class Apple : Fruit("Apple")
    class Mango : Fruit("Mango")
}

but in kotlin by default all classes are final this will also makes restricted the class hierarchies

below Example class cant extend Hi class because its final

class Hi {
    
}
class Example : Hi() {
    
}

so what makes the difference in between these two please let me know,

thank you in advance.

3

There are 3 answers

0
Simon Jacobs On

Yes, all regular classes in Kotlin are final. But you overlook the related structures of abstract classes and interfaces – and not to forget you can also make regular classes open with the open keyword. All these structures permit inheritance.

Ok, then given that inheritance is fully provided for in Kotlin with these structures, then the language goes on to provide sealed classes which limit the classes that can inherit from a given base class to a limited set.

0
broot On

Both your code samples actually show why we need them. If we need a Fruit class and then Apple and Mango, but we want to disallow any other fruit types, this is not possible without using a sealed type. Similarly, if we need Example extending Hi, but disallow any other subtypes of Hi.

If we make the class final, we can't create its subtypes. If we don't make it final, we can't control its subtypes - anyone can create one. Sealed types provide a possibility that we create subtypes, but others can't do it.

It also allows the compiler to "know" all possible subtypes, to make sure there are no other subtypes and as a result, make additional proofs or guarantees in the code. For example:

val name = when (fruit) {
    is Apple -> "apple"
    is Mango -> "mango"
}

In this case the compiler can proof there are no other possibilities than Apple or Mango, so we don't have to provide an else branch which would be redundant.

0
XGouchet On

The sealed keyword is used to make a class open to child implementation, but limiting the child class to only those declared in the same file. Kotlin classes are final by default, but you can still extend classes by making the parent one open, e.g.:

open class Vehicle

class Car: Vehicle

Making a class open means that any class could extend it.

On the other hand, a sealed class only allows being extended from the exact same file. This means that the sealed class will only ever have a limited and known list of children classes. It first ensures that no unknown extension can be used, but as mentioned by broot, it let's the Kotlin compiler ensure that all possible cases are handled in a when switch:

val name = when (fruit) {
    is Apple -> "apple"
    is Mango -> "mango"
}

If the Fruit class had been declared open instead of sealed, someone could eventually write a class Tomato: Fruit and feed it to a code that only expects Apples or Mangos, thus ruining the fruit salad.