Open In App

open Keyword in Kotlin

Last Updated : 10 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Keywords are some predefined or specific reserved words in programming languages each of which has a specific feature associated with it. Almost all of the words which help us use the functionality of the programming languages are included in the list of keywords. So you can imagine that the list of keywords is not going to be a small one, and the “open” keyword is also one of them, so this article is also based on open keyword. Now let’s try to understand the “open” keyword in kotlin. As we all know that kotlin is a modern and highly recommended language for android app development. because it really makes android app development easier a lot, and we all should know that it is also the modification of the java programming language, so why not we understand the “open” keyword by comparison with java. i.e what we can use instead of “open” keyword in java. So basically java has a keyword named “final“. but it works exactly opposite to the “open” keyword, so let’s quickly understand the “final” keyword. I hope you all know about it, but don’t worry if don’t.

the “final” keyword is used in java mostly with classes and methods (functions) as:

final method : A method that cannot be overridden.

final class : A class that cannot be extended.

But, Kotlin has a special feature i.e. classes and methods are not open for extension by default, which means they are by default final class or final function. It means Open classes and methods in Kotlin are equivalent to the opposite of final in Java, an open method is overridable and an open class is extendable in Kotlin.

Note: your class is implicitly declared as open since it is abstract, hence you cannot create an instance of that class directly.

Examples

Example 1: Extension  of class

Now we know that kotlin has everything final by default. So, if we try to extend the class then the compiler will show an error.

Kotlin




class Geeks
class student:Geeks()


error: This type is final, so it cannot be inherited

To make a class open for extension we should use “open” keyword as:

Kotlin




// mark the class with "open"
open class Geeks
class student:Geeks()


Example 2: Overriding of function(methods)

Same as classes, functions(Methods) can’t be overridable in their default configuration, even if the class containing function is open

Kotlin




open class geeks{
  fun student():string ="alok"
}
class batch: geeks() {
  override fun student():string = "ashish"
}


this code will show an error

error: 'student' in 'geeks' is final and cannot be overridden

To override a method in subclasses, we should use “open” keyword as:

Kotlin




open class geeks{
 // mark the function with open 
 open fun student():string ="alok"
}
class batch: geeks() {
  override fun student():string = "ashish"
}


Now our code is correct.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads