Kotlin Series — open Keyword in Kotlin — Android
What’s the purpose of Open keyword
This Story is from the Kotlin- Series, In which we will learn What is the purpose of Open Keyword with the help of an example. Well in Kotlin by default all the classes are final which means they are not- inheritable, so for Inheritance, we need to use a keyword open in your class. Let’s check with an example.
Let say we have a class called “ClassA” and we have a class called “ClassB”, now I want “ClassB” to inherit “ClassA”, so in kotlin If I inherit the class it shows me an error like below:
In Kotlin If I need to use inheritance I need to use the keyword “open” from which class I need to inherit like below:
After adding a keyword open there will be no error you can now easily use inheritance.
That’s it Happy Coding with Kotlin 😊😊
If you want to learn more related to Kotlin then check it out the below link: