Kotlin - Inheritance

What is Inheritance?


In Object Oriented Programming Inheritance is most important feature.

Inheritance is a mechanism in which one class acquires the properties other class.

It is a reusability concept, means we can use the one class properties in other class

Inheritance is a relation between Super & Sub classes


Example

class Child:Parent() {
    override
    fun bedRoom()
    {
        println("Child bedRoom")
    }
}

openclass Parent {
    
  open  fun bedRoom()
    {
        println("Parent bedRoom")
    }
}

Here Parent is super class and Child is sub class. Child class contains all methods of Parent class

Note : In kotlin all class are final, if we want to derive sub class we need to declare it as open

Every class in kotlin has default class Any. This Super class contains below methods

equals(), hashCode() and toString()

Subscribe For Daily Updates