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




PYTHON - 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:

    def breathe(self):
        print("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 :



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()


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( )'.


java_Collections

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.


class Human(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 'Human' class.


java_Collections

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


Now, after creating the 'human1' object,


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 :



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()


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'.


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(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)


java_Collections

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'.


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

java_Collections

And since, the 'breathe( )' method is also a part of the 'Human' class. 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( ) Function


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.


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()


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


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.


super( ).__init__(source)

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.