Kotlin - Sealed Class
In this chapter we speak Sealed Class of Kotlin programming language.A sealed magnificence, we need to apply the keyword "sealed" as a modifier of that elegance. A sealed magnificence can have its personal subclass however all the ones subclasses want to be declared within the identical Kotlin file at the side of the sealed class.
Let's see example
sealed magnificence MyExample class OP1 : MyExample() // MyExmaple magnificence can be of two kinds only elegance OP2 : MyExample() a laugh important(args: ArrayIn the above instance, we've one sealed magnificence named “MyExample”, which may be of sorts simplest - one is “OP1” and every other one is “OP2”. In the main class, we are developing an object in our class and assigning its kind at runtime. Now, as this “MyExample” elegance is sealed, we will apply the “whilst ” clause at runtime to put in force the final output.
) val obj: MyExample = MyExample.OP2() val output = whilst (obj) // defining the object of the magnificence depending at the inuputs is MyExample.OP1 -> "Option One has been chosen" is MyExample.OP2 -> "choice Two has been chosen" println(output)
option Two has been selected