Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - INHERITANCE


Inheritance is to reuse the Behaviors/methods and properties/state of a Parent class in child class.


Story :

God has created human beings. So, he has prepared the 'Human' class with states and behaviours. So that human beings could be created based on the stated behaviour.

Now let's say there is a supreme God(more powerful than the God who created Humans). He had decided how all living beings should behave(Human, Animal, Fish).

So, he created LivingBeing class.


class LivingBeing {

	fun breathe() {
		println("Breathes oxygen from air.")
	}
}

Story :

Now, the Supreme God(i.e. The creator of the above 'LivingBeing' class) made this rule that all living beings, be it Human or Animal everyone should breathe oxygen from air.

So, the God who has created Human is not going to define the breathe() behaviour again. And he decided to reuse it.

Now, LivingBeing is the parent class and Human class is going to be the child class.


Let us see in the below example.


Example :



open class LivingBeing {

    fun breathe() {
        println("Breathes oxygen from air")
    }
}

class Human(var name: String, var food: String, var language: String): LivingBeing() {

    fun eat() {
        println(name+" eats "+food)
    }

    fun speak() {
        println(name+" speaks "+language)
    }
}

fun main() {
    var human1 = Human("John", "Burger", "English")

    human1.eat()
    human1.speak()

    human1.breathe()
}


Output :



  John eats Burger
  John speaks English
  Breathes oxygen from air

So, in the above example, we have created a LivingBeing class that just has a Behaviour/Method i.e. breathe().


open class LivingBeing {
	fun breathe() {
		println("Breathes oxygen from air")
	}
}

And used the open keyword in front of the LivingBeing class. So that it can be inherited.


open class LivingBeing {
java_Collections


Then we have created the Human class and the Human class should have the breathe() method from the LivingBeing class.


And since, the breathe() method is defined in the LivingBeing class. There is no point in redefining the breathe() method again.


Rather the Human class reuses it by inheriting all the members of the LivingBeing class.


class Human(var name: String, var food: String, var language: String): LivingBeing() {

The syntax is quite simple. We just place the class to be inherited(i.e. LivingBeing) inside the brackets of class definition.

java_Collections

And what happens is, breathe() method of LivingBeing class becomes a part of the Humanclass.

java_Collections

And along with eat() and speak() method, breathe() method also becomes a part of Human class.


Now, after creating the human1 object,


var human1 = Human("John", "Burger", "English")
java_Collections


Now when we call the breathe() method from human1 object.


human1.breathe()

We get the below output.


Breathes oxygen from air

Inheriting Methods and Attributes


In the above example, we have seen that we have inherited the Methods of the Parent class (i.e. LivingBeing) in the Child class (i.e. Human).


This time, let us modify the LivingBeing class a little. What we will do is, add a new attribute named source to the Parent class, LivingBeing.


Example :



open class LivingBeing(sourceTemp: String) {

    var source: String = sourceTemp

    fun breathe() {
        println("Breathes oxygen from "+source)
    }
}

class Human(nameTemp: String, foodTemp: String, languageTemp: String, sourceTemp: String): LivingBeing(sourceTemp) {

    var name: String = nameTemp
    var food: String = foodTemp
    var language: String = languageTemp

    fun eat() {
        println(name+" eats "+food)
    }

    fun speak() {
        println(name+" speaks "+language)
    }
}

fun main() {
    var human1 = Human("John", "Burger", "English", "Air")

    human1.eat()
    human1.speak()

    human1.breathe()
}


Output :



  John eats Burger
  John speaks English
  Breathes oxygen from Air

So in the above example, we have added a new attribute named source to the Parent class, LivingBeing along with the breathe() method.


open class LivingBeing(sourceTemp: String) {
	var source: String = sourceTemp
	fun breathe() {
		println("Breathes oxygen from "+source)
	}
}
java_Collections


Then we have declared the Human class inheriting all the contents of the LivingBeing class. And the attribute (i.e. source) and the method (i.e. breathe()) of LivingBeing class becomes a part of the Human class.


class Human(nameTemp: String, foodTemp: String, languageTemp: String, sourceTemp: String): LivingBeing(sourceTemp) {

	var name: String = nameTemp
	var food: String = foodTemp
	var language: String = languageTemp

	fun eat() {
		println(name+" eats "+food)
	}

	fun speak() {
		println(name+" speaks "+language)
	}
}
java_Collections


So, the constructor of Human class,


'class Human(nameTemp: String, foodTemp: String, languageTemp: String, sourceTemp: String): LivingBeing(sourceTemp)'

has sourceTemp as an argument.


Then, we have created the object human1 passing the values, "John", "Burger", "English" along with the 4th argument, i.e. The value "Air" as the value for attribute source.


var human1 = Human("John", "Burger", "English", "Air")
java_Collections


And since, the breathe() method is also a part of the Human class(By Inheritance). We can call the breathe() method from the human1 Object.


human1.breathe()

And we get the below output.


Breathes oxygen from Air

So, we have seen, not just the methods but the attributes are also inherited.


Use of super Keyword


The super Keyword is used in the child class to call the Constructor of its Parent class.


To understand super Keyword, let us take the example of secondary constructor to achieve it.


Let's simplify with the below example.


Example :



open class LivingBeing {

    var source: String = ""

    constructor(sourceTemp: String) {

        source = sourceTemp
    }

    fun breathe() {
        println("Breathes oxygen from "+source)
    }
}

class Human: LivingBeing {

    var name: String = ""
    var food = ""
    var language = ""
    
    constructor(nameTemp: String, foodTemp: String, languageTemp: String, sourceTemp: String): super(sourceTemp) {
        name = nameTemp
        food = foodTemp
        language = languageTemp
    }

    fun eat() {
        println(name+" eats "+food)
    }

    fun speak() {
        println(name+" speaks "+language)
    }
}

fun main() {
    var human1 = Human("John", "Burger", "English", "Air")

    human1.eat()
    human1.speak()

    human1.breathe()
}


Output :



  John eats Burger
  John speaks English
  Breathes oxygen from Air

So in the above example, we have an attribute named source and a method breathe() in the Parent class, LivingBeing.

java_Collections

open class LivingBeing {
	var source: String = ""

	constructor(sourceTemp: String) {
		source = sourceTemp
	}

	fun breathe() {
		println("Breathes oxygen from "+source)
	}
}

And we wanted to initialise the source attribute in the constructor(sourceTemp: String) constructor of LivingBeing.


But as we know, the constructor(sourceTemp: String) constructor is only called at the time of Object creation.


And we won't be creating any objects of LivingBeing.


And this is where super comes to picture.


Let us see, how can we use the super to call the constructor(sourceTemp: String) constructor.


Then we have declared the Human class inheriting all the contents of the LivingBeing class.


class Human: LivingBeing {

	var name: String = ""
	var food = ""
	var language = ""

	constructor(nameTemp: String, foodTemp: String, languageTemp: String, sourceTemp: String): super(sourceTemp) {
		name = nameTemp
		food = foodTemp
		language = languageTemp
	}

	fun eat() {
		println(name+" eats "+food)
	}

	fun speak() {
		println(name+" speaks "+language)
	}
}

Now, in the __init__() constructor of the Human class,


constructor(nameTemp: String, foodTemp: String, languageTemp: String, sourceTemp: String): super(sourceTemp) {
	name = nameTemp
	food = foodTemp
	language = languageTemp
}

We have called the constructor(sourceTemp: String) constructor of the HumanBeing class usingsuper.

java_Collections

And the value of the attribute source gets set in the constructor(sourceTemp: String) constructor of the Parent class (i.e. HumanBeing).


constructor(sourceTemp: String) {
	source = sourceTemp
}