In this chapter, we will learn about inheritance. By definition, we all know that inheritance means accruing some properties of the mother class into the child class. In Kotlin, the base class is named as “Any”, which is the super class of the ‘any’ default class declared in Kotlin. Like all other OOPS, Kotlin also provides this functionality using one keyword known as “:”.
Everything in Kotlin is by default final, hence, we need to use the keyword “open” in front of the class declaration to make it allowable to inherit. Take a look at the following example of inheritance.
Live Demo
import java.util.Arrays
open class ABC { fun think () { print(“Hey!! i am thiking “) } } class BCD: ABC(){ // inheritence happend using default constructor }
fun main(args: Array
Live Demo
import java.util.Arrays
open class ABC {
open fun think () {
print(“Hey!! i am thinking “)
}
}
class BCD: ABC() { // inheritance happens using default constructor
override fun think() {
print(“I Am from Child”)
}
}
fun main(args: Array