Techoceanhub logo

What is override and open keyword in Kotlin(Chapter-11)

In the previous post we have discussed ‘open’ and ‘override’ keywords in Kotlin, but now let us discuss them in detail. In inheritance, it is very important to use these 2 keywords in Kotlin, without them inheritance is not fully supported. First, let us discuss what is an open keyword, which is quite popular in maintaining inheritance otherwise, inheritance is not possible without this keyword.

open: In Kotlin, all the classes are final by default i.e. they can’t be inherited as final keyword signifies. Let us see the basic syntax of the open keyword.

//Parent Class
open class Parent {

}

//Child class
class Child : Parent(){

}

Override: This keyword is very popular in inheritance in kotlin. A child class can override the data and member function of the parent class using the override keyword. By overriding a function, a child class gives its own implementation to the already existing code of the parent class. Let us see the basic syntax of the override keyword in kotlin.

open class Parent {
    open fun printData() { /*...*/ }
    fun write() { /*...*/ }
}

class Child() : Parent() {
    override fun printData() { /*...*/ }
}

The above is a basic example of override but you cannot use the override keyword without an open keyword because unless you declare an “open” keyword in the base class method or variable, we cannot override that variable or a method. There is another scenario also if you do not want to further override that method in your child classes then you have to use the “final” keyword before override keyword, Let us see how that syntax looks in the below sample code.

open class Parent {
    open fun printData() { /*...*/ }
    fun write() { /*...*/ }
}

class Child() : Parent() {
     override fun printData() { /*...*/ }
}
class subChild() : Child() {
    final override fun printData() { /*...*/ }
}

Now, in this case, you cannot override printData() method of subchild class as the final keyword prevents further overriding.

Feel free to read the post and comment below.

Below is my android application, and is useful for people who has Google Opinion Rewards to cash out. https://play.google.com/store/apps/details?id=com.manasvi.sawant.rewardtocash

If you wanted to create a website, please visit my Fiverr gig link below or contact me at support@techoceanhub.com. https://www.fiverr.com/share/EdZ9L7

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .