Open keyword in Kotlin
- Authors
- Name
- Amit Shekhar
- Published on
In Kotlin, all the classes, functions, and variables are final by default, which means they cannot be inherited or overridden. We use the open keyword to allow that.
In this blog, we will learn about the open keyword in Kotlin.
I am Amit Shekhar, Founder @ Outcome School, 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.
I teach AI and Machine Learning, and Android at Outcome School.
Join Outcome School and get a high-paying tech job:
Let's get started.
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 Java | In 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.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
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.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
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.
To learn Kotlin features like the open keyword, data class, extension functions, and inheritance in depth, check out the Android Program by Outcome School.
Watch the video format: open keyword in Kotlin
Frequently Asked Questions
Are classes in Kotlin final by default?
Yes. In Kotlin, all the classes are final by default, meaning that they can not be inherited. If we try to create a subclass, the compiler shows the error "This type is final, so it cannot be inherited from". To allow inheritance, we mark the class with the open keyword.
How is the open keyword in Kotlin different from Java?
It is exactly the opposite of Java. In Java, classes, functions, and variables are open by default, and we use the final keyword to make them final. In Kotlin, they are final by default, and we use the open keyword to allow inheritance or overriding.
If a class is open, can we override all of its functions?
No. Making a class open only allows us to create a subclass of it. The functions inside it are still final by default. To override a function in the child class, we must also mark that function with the open keyword in the base class.
Can we override a variable in Kotlin?
Yes, but only if the variable is marked open in the base class. Variables in Kotlin are final by default, so without open, the compiler shows an error that the variable is final and cannot be overridden. With open, the child class can override it using the override keyword.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
