Subscribe Now

* You will receive the latest news and updates on your favorite celebrities!

Trending News

Blog Post

What is Open Class in Android Kotlin?
Kotlin

What is Open Class in Android Kotlin?

In this blog, we are going to talk about the “open” keyword in Kotlin. and How we can use the “open” keyword while making Android apps using Kotlin. You will also get to know Why you should use the “open” keyword with the class name, function name, and variable name.  If you want to learn Kotlin for Android Application Development then you can watch full Kotlin Videos on my this Youtube Channel.

What is the “open” Keyword in Kotlin?

In Kotlin, all the classes are final by default. So, they can’t be inherited by default. Whereas if you have worked with JAVA you have to explicitly make your class final.

So, to make a class inheritable to other classes in Kotlin you have to mark that class as open otherwise you will get an error saying “type is final so can’t be inherited”.

How to declare an open class?

open class BaseActivity

Inheritance in Kotlin using “open” Keyword

Following is the example of Inheritance in Kotlin

open class BaseActivity : AppCompatActivity() {
      //Base Class
}

class MainActivity : BaseActivity() {
      //Derived Class
}

In the above example, we are inheriting the class BaseActivity from the class MainActivity. So, BaseActivity is the Parent class and MainActivity is the Child Class.

Overriding Functions in Kotlin using “open” Keyword

All the functions we declare in Kotlin are final by default. As you all know we can’t override a function when the function is final in nature.

Function Overriding is a process of redefining the functions of a Base class in a Child class. Also, in the child class, you have to use the override modifier.

Following is an example of a function overriding in Kotlin:

open class BaseActivity : AppCompatActivity() {

    open fun userInfo() {
        Toast.makeText(this, "Base Activity", Toast.LENGTH_LONG).show()
    }

}

class MainActivity : BaseActivity() {

    override fun userInfo() {
        Toast.makeText(this, "Base Activity", Toast.LENGTH_LONG).show()
    }

}

In the above example, the Parent class BaseActivity is having a function named userInfo() and this BaseActivity class is inherited by the MainActivity class. In the MainActivity class, we are overriding the method userInfo() and we are redefining the body of the function.

“open” Keyword with Variables

The variables in Kotlin are by default final in nature. So, If you want to override variable in child class you need to declare a variable as “open” in the base class.

Following is an example of the same:

open class BaseActivity : AppCompatActivity() {
    
    open val userID: Long = 0

}

class MainActivity : BaseActivity() {

    override val userID: Long = 1001

}

In the above example, the userId in BaseActivity class is set to 0 but in the child class if you want to change the value of userId then you can override userId and assign a new value to the variable. Like we assigned as 1001.

Conclusion

In Kotlin, the classes, the variables, and the functions are final by default. They can’t inherit from other classes. So to make inherit from other classes we use the “open” keyword in Kotlin with the class, function, and variable name.

If you don’t know what is Class and Object in Kotlin then read my blog regarding How to create a Class and Object?

Watch Youtube video for more information regarding “open” Keyword in Kotlin:

I hope you like this blog. If you like it then share this with your friends or colleagues. You can contact me on Facebook, Twitter, or on Instagram also.

Related posts

Leave a Reply

Required fields are marked *