Skip to content

Inheritance in Python




Python – Advanced OOP



As we read earlier about Python basic OOP , Now we will study about the Inheritance in Python . Inheritance is the most important concept in Object Oriented Programming. so let’s start

In the advanced concept of OOP , there are 4 principles of object – oriented programming are :-

  1. Inheritance

  2. Polymorphism

  3. Encapsulation

  4. Abstraction

It is also known as the “Four pillars of object – oriented programming”.

Now, let’s study our first principle i.e. Inheritance.





What is Inheritance in python ?

Inheritance is the first principle of object oriented programming and it is used to define a class that inherits all the methods and properties from another class.

In python programming, a derived class can inherit base class by introducing the base class name in the bracket after the derived class name.



Python Inheritance Syntax :

class BaseClass:
     statement of base class

class DerivedClass(BaseClass):
     statement of derived class



Python Inheritance Example :

# Create a Parent Class or Base Class named Class1
class Class1:
    def __init__(self):                   # This is a Constructor
        self.a=1
        self.b=11
    def show_data(self):
        print(self.a, self.b)

# Create a Child Class or Derived Class named Class2
class Class2(Class1):
    pass

obj=Class2()                              # Create an object of Class2
obj.show_data() 
print()




What is Base Class in python ?

Base Class is the class being inherited from is called as python base class and It is also known as Parent Class or Super Class.




What is Derived Class in python ?

Derived Class is the class being inherited from the another class is called as derived class and It is also known as Child Class or Sub Class.





What is super( ) function in python ?

super is a keyword that is used to refer object of immediate parent class is called super function. super always attached with “self“.




Python Inheritance super example :

# Create a Base Class or Parent Class named Class1
class Class1:
    def __init__(self):           # This is a Constructor
        self.a = 1
        self.b = 11

    def showData(self):
        print(self.a,self.b)

# Create a Derived Class or Child Class named Class2
class Class2(Class1):
    def __init__(self):
        self.c = 2
        self.d = 22
        super().__init__()       # Use super() to inherit the constructor of Class1

    def showData(self):
        print(self.c,self.d)
        super().showData()       # Use super() to inherit showData() function of Class1

ob1=Class1()                     # Create an Object of Class1              
ob2=Class2()                     # Create an Object of Class2
ob2.showData()
print()




Types of Inheritance in python

Python supports Five different types of Inheritance are : – 



Single Inheritance

Single Inheritance means when a child class (derived class) inherits from only one parent class (base class) is called single inheritance. It is also known as Simple Inheritance.



Flowchart of Single Inheritance :

flowchart-of-single-inheritance


Single inheritance example : Single Inheritance with Super function.

# Create a Base Class or Parent Class named Class1
class Class1:
    def __init__(self):                    # This is a Constructor
        self.a = 1
        self.b = 11
    def __str__(self):
        return "a : " + str(self.a) + "\n" + "b : " + str(self.b)    # This is a __str__ function
    def show_data(self):
        print(self.a, self.b)

# Create a Derived Class or Child Class named Class2
class Class2(Class1):
    def __init__(self):
        self.c = 2
        self.d = 22
        super().__init__()                  # Use super() to inherit the constructor of Class1
    def __str__(self):
        return "c : " + str(self.c) + "\n" + "d : " + str(self.d) + "\n" + super().__str__()                 # Use super() to inherit the str function of Class1
    def show_data(self):
        print(self.c, self.d)
        super().show_data()                # Use super() to inherit the show_data function of Class1

C1 = Class1()                              # Create an Object of Class1
C2 = Class2()                              # Create an Object of Class2
print(C2)





Multi – level Inheritance

Multi-level Inheritance means when we have a child and grandchild relationship is called multi level inheritance.  



Flowchart of Multi – level Inheritance :

flowchart-of-multi-level-inheritance



Multi-level inheritance example : Multi – Level Inheritance with Super function.

# Create a Base Class or Parent Class named Class1
class Class1:
    def __init__(self):             # This is a Constructor
        self.a = 1
        self.b = 11
    def __str__(self):
        return "a : " + str(self.a) + "\n" + "b : " + str(self.b) # This is __str__ function
    def show_data(self):
        print(self.a, self.b)

# Create a Derived Class or Child Class named Class2
class Class2(Class1):
    def __init__(self):
        self.c = 2
        self.d = 22
        super().__init__()         # Use super() to inherit the constructor from Class1
    def __str__(self):
        return "c : " + str(self.c) + "\n" + "d : " + str(self.d) + "\n" + super().__str__()      # Use super() to inherit the str function from Class1
    
    def show_data(self):
        print(self.c, self.d)
        super().show_data()        # Use super() to inherit the show_data function from Class1

# Create an GrandChild Class named Class3
class Class3(Class2):
    def __init__(self):
        self.e = 3
        self.f = 33
        super().__init__()         # Use super() to inherit the constructor from Class2
    def __str__(self):
        return "e : " + str(self.e) + "\n" + "f : " + str(self.f) + "\n" + super().__str__()         # Use super() to inherit the str function from Class2
    
    def show_data(self):
        print(self.e, self.f)
        super().show_data()        # Use super() to inherit the show_data function from Class2

C1 = Class1()                      # Create an Object of Class1
C2 = Class2()                      # Create an Object of Class2
C3 = Class3()                      # Create an Object of Class3
print(C3)





Multiple Inheritance

Multiple Inheritance means when a child class (derived class) inherits from multiple parent class (base class) is called multiple inheritance.



Flowchart of Multiple Inheritance :

flowchart-of-multiple-inheritance



Multiple inheritance example : Multiple Inheritance with Super function.

# Create a Base Class or Parent Class named Class1
class Class1:
    def __init__(self):               # This is a Constructor
        self.a = 1
        self.b = 11
    def __str__(self):
        return "a : " + str(self.a) + "\n" + "b : " + str(self.b) + "\n"     # This is __str__ function
    def show_data(self):
        print(self.a, self.b)

# Create an another Base Class or Parent Class named Class2
class Class2:
    def __init__(self):
        self.c = 2
        self.d = 22
    def __str__(self):
        return "c : " + str(self.c) + "\n" + "d : " + str(self.d) + "\n"
    def show_data(self):
        print(self.c, self.d)

# Create a Derived Class or Child Class named Class3
class Class3(Class2,Class1):
    def __init__(self):
        self.e = 3
        self.f = 33
        super().__init__()            # Use super() to inherit the constructor from Class2
        Class1.__init__(self)         # Use Class Name to inherit the Constructor from Class1
    def __str__(self):
        return "e : " + str(self.e) + "\n" + "f : " + str(self.f) + "\n" + super().__str__() + 
        Class1.__str__(self)     # Use super() to inherit the str function from Class2 and To use Class Name to inherit the str function from Class1
    def show_data(self):
        print(self.e, self.f)
        super().show_data()            # Use super() to inherit the show_data function from Class2
        Class1.show_data(self)         # Use Class Name to inherit the show_data function from Class1

C1 = Class1()                          # Create an Object of Class1
C2 = Class2()                          # Create an Object of Class2
C3 = Class3()                          # Create an Object of Class3
print(C3)





Hierarchical Inheritance

Hierarchical Inheritance means more than one derived class (child class) are created from a single base is called hierarchical inheritance.



Flowchart of Hierarchical Inheritance :

flowchart-of-hierarchical-inheritance



Hierarchical inheritance example : Hierarchical Inheritance with Super function.

# Create a Base Class or Parent Class named Class1
class Class1:
    def __init__(self):           # This is a Constructor
        self.a = 1
        self.b = 11
    def __str__(self):
        return "a : " + str(self.a) + "\n" + "b : " + str(self.b)     # This is __str__ function
    def show_data(self):
        print(self.a, self.b)

# Create a Derived Class or Child Class named Class2
class Class2(Class1):
    def __init__(self):
        self.c = 2
        self.d = 22
        super().__init__()         # Use super() to inherit the constructor from Class1
    def __str__(self):
        return "c : " + str(self.c) + "\n" + "d : " + str(self.d) + "\n" + super().__str__()         # Use super() to inherit the str function from Class1
    
    def show_data(self):
        print(self.c, self.d)
        super().show_data()        # Use super() to inherit the show_data function from Class1

# Create an another Derived Class named Class3
class Class3(Class1):
    def __init__(self):
        self.e = 3
        self.f = 33
        super().__init__()         # Use super() to inherit the constructor from Class1
    def __str__(self):
        return "e : " + str(self.e) + "\n" + "f : " + str(self.f) + "\n"+ super().__str__()        # Use super() to inherit the str function from Class1
    
    def show_data(self):
        print(self.e, self.f)
        super().show_data()        # Use super() to inherit the show_data function from Class1

C2 = Class2()
print(C2)                          # To handle CLASS2 and CLASS1 Properties

C3 = Class3()
print(C3)                          # To handle CLASS3 and CLASS1 Properties





Hybrid Inheritance

Hybrid Inheritance involves multiple inheritance taking place in a single program is called hybrid inheritance.



Flowchart of Hybrid Inheritance :

flowchart-of-hybrid-inheritance



Hybrid inheritance example : Hybrid Inheritance with Super function

# Create a Base Class or Parent Class named Class1
class Class1:
    def __init__(self):                # This is a Constructor
        self.a=1 
        self.b=11
    def __str__(self):
        return "a : "+ str(self.a) + "\n" + "b : " +str(self.b) + "\n"     # This is __str__ function
    def showData(self):
        print(self.a,self.b)

# Create a Derived Class or Child Class named Class2
class Class2(Class1):
    def __init__(self):
        self.c=2
        self.d=22
        super().__init__()            # Use super() to inherit the constructor from Class1

    def __str__(self):
        return "c : "+str(self.c)+ "\n" + "d : " + str(self.d) + "\n" + super().__str__()         # Use super() to inherit the str function from Class1
    def showData(self):
        print(self.c,self.d)
        super().showData()            # Use super() to inherit the show_data function from Class1

# Create an another Base Class or Parent Class named Class4
class Class4:
    def __init__(self):
        self.g = 4
        self.h = 44
    def __str__(self):
        return "g : " + str(self.g)+ "\n" + "h : " +str(self.h)
    def showData(self):
        print(self.g,self.h)

# Create a Derived Class or Child Class named Class3
class Class3(Class2,Class4):
    def __init__(self):
        self.e = 3
        self.f = 33
        super().__init__()            # Use super() to inherit the constructor from Class2
        Class4.__init__(self)         # Use Class Name to inherit the constructor from Class4

    def __str__(self):
        return "e : " + str(self.e) +"\n" + "f : " + str(self.f) + "\n" + super().__str__() + 
        Class4.__str__(self)       # Use super() to inherit the str function from Class2 and To use Class Name to inherit the str funtion from Class4

    def showData(self):
        print(self.e, self.f)
        super().showData()            # Use super() to inherit the show_data function from Class2
        Class4.showData(self)         # Use Class Name to inherit the show_data function from Class4


ob1=Class1()                          # Create an Object of Class1
ob2=Class2()                          # Create an Object of Class2
ob3=Class3()                          # Create an Object of Class3
ob4=Class4()                          # Create an Object of Class4
print(ob3)



>