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.
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
openkeyword. 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.