Kotlin Sealed ClassSealed class is a class which restricts the class hierarchy. A class can be declared as sealed class using "sealed" keyword before the class name. It is used to represent restricted class hierarchy. Sealed class is used when the object have one of the types from limited set, but cannot have any other type. The constructors of sealed classes are private in default and cannot be allowed as non-private. Declaration of sealed classThe subclasses of sealed classes must be declared in the same file in which sealed class itself. Sealed class ensures the important of type-safety by restricting the set of types at compile time only. A sealed class is implicitly an abstract class which cannot be instantiated. Sealed class with whenSealed classes are commonly used with when expression. As the sub classes of sealed classes have their own types act as a case. Due to this, when expression in sealed class covers all the cases and avoid to add else clause. For example: Output: Circle area is 78.5 Square area is 25 Rectagle area is 20 Next TopicKotlin Extension Function |