Inheritance is to reuse the Behaviors/methods and properties/state of a Parent class in child class.
So, he created 'LivingBeing' class.
class LivingBeing: def breathe(self): print("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.
class LivingBeing: def breathe(self): print("Breathes oxygen from air") class Human(LivingBeing): def __init__(self, name, food, language): self.name = name self.food = food self.language = language def eat(self): print(self.name," eats ",self.food) def speak(self): print(self.name," speaks ",self.language) 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( )'.
Then we have created the 'Human' 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.
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 'Human' class.
And along with 'eat( )' and 'speak( )' method, 'breathe( )' method also becomes a part of 'Human' class.
Now, after creating the 'human1' object,
Now when we call the 'breathe( )' method from 'human1' object.
We get the below output.
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'.
class LivingBeing: def breathe(self): print("Breathes oxygen from ",self.source) class Human(LivingBeing): def __init__(self, name, food, language, source): self.source = source self.name = name self.food = food self.language = language def eat(self): print(self.name," eats ",self.food) def speak(self): print(self.name," speaks ",self.language) 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'.
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(LivingBeing): def __init__(self, name, food, language, source): self.source = source self.name = name self.food = food self.language = language def eat(self): print(self.name," eats ",self.food) def speak(self): print(self.name," speaks ",self.language)
So, the '__init__(self, name, food, language, source)' constructor of 'Human' class, has 'source' as an argument.
def __init__(self, name, food, language, source): self.source = source self.name = name self.food = food self.language = language
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'.
And since, the 'breathe( )' method is also a part of the 'Human' class. We can call the 'breathe( )' method from the 'human1' Object.
And we get the below output.
So, we have seen, not just the methods but the attributes are also inherited.
The 'super( )' Function is used in the child class and is used to call a method of its Parent class.
We have seen the above example. Let us say the Parent class, 'LivingBeing' has an '__init__( )' method and we want to initialise its 'source' attribute there.
Sounds Complex ?
Let's simplify with the below example.
class LivingBeing: def __init__(self, source): self.source = source def breathe(self): print("Breathes oxygen from ",self.source) class Human(LivingBeing): def __init__(self, name, food, language, source): super().__init__(source) self.name = name self.food = food self.language = language def eat(self): print(self.name," eats ",self.food) def speak(self): print(self.name," speaks ",self.language) 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'.
class LivingBeing: def __init__(self, source): self.source = source def breathe(self): print("Breathes oxygen from ",self.source)
And we wanted to initialise the 'source' attribute in the '__init__( )' constructor of 'LivingBeing'.
But as we know, the '__init__( )' 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( )' function comes to picture.
Let us see, how can we use the 'super( )' function to call the '__init__( )' constructor.
Then we have declared the 'Human' class inheriting all the contents of the 'LivingBeing' class.
class Human(LivingBeing): def __init__(self, name, food, language, source): super().__init__(source) self.name = name self.food = food self.language = language def eat(self): print(self.name," eats ",self.food) def speak(self): print(self.name," speaks ",self.language)
Now, in the '__init__( )' constructor of the 'Human' class,
def __init__(self, name, food, language, source): super().__init__(source) self.name = name self.food = food self.language = language
We have called the '__init__( )' constructor of the 'HumanBeing' class using the 'super( )' Function.
And the value of the attribute 'source' gets set in the '__init__( )' constructor of the Parent class 'HumanBeing'.
def __init__(self, source): self.source = source
So, just the way 'super( )' Function is used to call the '__init__( )' Constructor of the Parent,'HumanBeing' class from the child, 'Human' class.
Similarly, the 'super( )' Function can be used to call any method of the parent class.