Inheritance is to reuse the Behaviors/methods and properties/state of a Parent class in child class.
So, he created LivingBeing class.
class LivingBeing { fun breathe() { println("Breathes oxygen from air.") } }
Now, LivingBeing is the parent class and Human class is going to be the child class.
Let us see in the below 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() }
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 {
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.
And what happens is, breathe() method of LivingBeing class becomes a part of the Humanclass.
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")
Now when we call the breathe() method from human1 object.
human1.breathe()
We get the below output.
Breathes oxygen from air
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.
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() }
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) } }
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) } }
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")
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.
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.
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() }
So in the above example, we have an attribute named source and a method breathe() in the Parent class, LivingBeing.
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.
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 }