AndroidPub
Published in

AndroidPub

Kotlin Series — open Keyword in Kotlin — Android

What’s the purpose of Open keyword

Image Source

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:

Android Studio ScreenShot

In Kotlin If I need to use inheritance I need to use the keyword “open” from which class I need to inherit like below:

Android Studio ScreenShot

After adding a keyword open there will be no error you can now easily use inheritance.

Android Studio ScreenShot

That’s it Happy Coding with Kotlin 😊😊

If you want to learn more related to Kotlin then check it out the below link:

Sharing the knowledge is Caring 😊

Thanks for reading this article. Be sure to clap to recommend this article if you found it helpful. It means a lot to me.

If you need any help then Join me on Twitter, Linkedin, Github, and Subscribe to my Youtube Channel.

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store