In this document, we will explain how and whyopen
Keywords are used with class names, function names, and variable names.
We all know that Inheritance is the backbone of every object-oriented programming language. It is a process in which properties and features of one class are derived or used by another class. Simply put, if a class named ClassA has some variables and functions, and another class named ClassB inherits ClassA, then ClassB will use ClassA's variables and methods. Here, ClassA is called a parent class and ClassB is called a subclass. Inheritance makes reusable possible.
1 Kotlin open usage in class name
In Kotlin, to inherit a class from another class, you can use the following syntax:
//Base Class open class MindOrks { } //Derived class class AndroidPro : MindOrks(){ }
In the example above, weAndroidPro
Class inheritanceMindOrks
kind. therefore,MindOrks
It's the parent class,AndroidPro
It is a subclass.
In Kotlin, all classes are final by default, i.e. they cannot be inherited by default. This is the opposite of what we learned in Java. In Java, we must explicitly set the class to final.
Therefore, to make a class inheritable by other classes, we must mark it with the open keyword here, otherwise an error will be reported:type is final so can’t be inherited
。
2 The use of Kotlin open in function name
Just like a class, all functions in Kotlin are ultimately final by default, that is, when the function is ultimately final, we cannot override a function.
The override of a function is the process of redefining the base class function in a subclass. Therefore, keywords need to be added before the corresponding function of the parent classopen
, Meanwhile, before this function in the subclass, we must use the override modifier.
open class MindOrks { //use open keyword to allow child class to override it open fun courseName(){ println("Course Name") } } class AndroidPro : MindOrks(){ //use the override keyword to override the function override fun courseName() { println("Android for Professionals") } }
In this example,MindOrks
The parent class has a namecourseName()
function, and thisMindOrks
Classes byAndroidPro
Class inheritance. existAndroidPro
In the class, we rewritten itcourseName()
Method and redefined the body of the function.
3. Kotlin open use in variable names
Just like classes and functions, variables in Kotlin are essentially final by default. So to override it in the subclass, we need to set the variable toopen
:
open class MindOrks { //use open keyword to allow child class to override it open val courseId: Int = 0 //use open keyword to allow child class to override it open fun courseName(){ println("Course Name") } } class AndroidPro : MindOrks(){ //use the override keyword to override the variable override val courseId: Int = 1 //use the override keyword to override the function override fun courseName() { println("Android for Professionals") } }
In the example above,MindOrks
In the classcourseId
Set to 0, but in a subclass, i.e.AndroidPro
In the class, the value is changed to 1.
A simple summary: In Kotlin, classes, functions, and variables are essentially final by default, i.e. they cannot be inherited from any other class. Therefore, to make it inheritable from other classes, we use the open keyword with class, function, and variable names.
This is the introduction to this article about the use of open keywords, class names, function names and variable names in Kotlin development. For more related Kotlin open keyword content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!