Open keyword in Kotlin

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
Open keyword in Kotlin

I am Amit Shekhar, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Join my program and get high paying tech job: amitshekhar.me

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

In this blog, we will learn about the open keyword in Kotlin.

In Kotlin, we can mark a class, a function, or a variable with the open keyword like below:

Class

open class Mentor {

}

Function

open fun guide() {

}

Variable

open val slotsAvailable = 5

Let's understand why we use the open keyword in Kotlin.

open keyword with class

The open keyword with the class means the class is open for the extension meaning that we can create a subclass of that open class.

In Kotlin, all the classes are final by default meaning that they can not be inherited.

Suppose we have a class Mentor:

class Mentor {

}

We will not be able to create a subclass of the class.

class ExperiencedMentor: Mentor() {

}

The compiler will show an error.

This type is final, so it cannot be inherited from

If we want to allow inheritance, we need to mark the class with the open keyword. Basically, opening it for the extension.

Our updated Mentor class:

open class Mentor {

}

Now, we will be able to create a subclass of this class.

class ExperiencedMentor: Mentor() {

}

It will work perfectly.

You must have noticed that it is completely opposite to what we do in Java.

Let's see how it is different in Kotlin and Java.

In JavaIn Kotlin
final class Mentor { }->class Mentor { }
class Mentor { }->open class Mentor { }

Basically, in Kotlin, all the classes are final by default but in Java, this is exactly the opposite. In Java, to make the class final, we need to use the final keyword.

That is why we need the open keyword in Kotlin with the class to allow inheritance.

open keyword with function

Similar to the classes, all the functions in Kotlin are by default final meaning that you can't override a function.

Consider a function guide() inside the Mentor class:

open class Mentor {

    fun guide() {

    }

}

We will not be able to override the function.

class ExperiencedMentor : Mentor() {

    override fun guide() {

    }

}

The compiler will show an error.

guide' in 'Mentor' is final and cannot be overridden

In order to allow the overriding of the function, we need to add the open keyword.

Our updated Mentor class:

open class Mentor {

    open fun guide() {

    }

}

We will be able to override the function now like below:

class ExperiencedMentor : Mentor() {

    override fun guide() {

    }

}

It will work perfectly.

Again, in Kotlin, all the functions are final by default but in Java, this is exactly the opposite. In Java, to make the function final, we need to use the final keyword.

That is why we need the open keyword in Kotlin with the function to allow it to be overridden.

open keyword with variable

Similarly, the variables in Kotlin are final by default. So, to override it in the child class, we need to set the variables as open in our base class.

Here is the base class Mentor:

open class Mentor {

    val slotsAvailable = 5

}

Here is the child class.

class ExperiencedMentor : Mentor() {

    override val slotsAvailable = 10

}

We will not be able to override the variable.

The compiler will show an error.

'slotsAvailable' in 'Mentor' is final and cannot be overridden

In order to allow the overriding of the variable, we need to add the open keyword.

Our updated Mentor class:

open class Mentor {

    open val slotsAvailable = 5

}

We will be able to override the variable now like below:

class ExperiencedMentor : Mentor() {

    override val slotsAvailable = 10

}

It will work perfectly.

Again, in Kotlin, all the variables are final by default but in Java, this is exactly the opposite. In Java, to make the variable final, we need to use the final keyword.

That is why we need the open keyword in Kotlin with the variable to allow it to be overridden.

So, now we must have understood the open keyword in Kotlin.

Watch the video format: open keyword in Kotlin

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar

You can connect with me on:

Read all of my high-quality blogs here.