Types of Python Inheritance

Python Inheritance allows us to define a class that inherits all the methods and properties from another class. Inheritance means parent-child relationship. In this tutorial, we will be discussing all the 5 different types of inheritance in Python.

It is a powerful feature in object oriented programming. It is also referred to as reusability of the code so by using Inheritance we can reuse the code again and again.

You may read more on inheritance from the below tutorials.

There are 5 Types of inheritance in Python

  1. Single inheritance.
  2. Multiple inheritance
  3. Multilevel inheritance
  4. Hierarchical inheritance
  5. Hybrid inheritance

Single inheritance in Python

*Only one base class and one derived class is called Single inheritance.

father->child

Example-

class Country:
     def ShowCountry(self):
         print("This is India");

class State(Country):
     def ShowState(self):
         print("This is State");
   

st =State();
st.ShowCountry();
st.ShowState();
 

Output-

This is India
This is State

Multiple inheritance

*When a derived class contains more than one base class is called Multiple inheritance.

# Example for multiple inheritance

class stud:
    def method1(self,sno,sname):
        self.sno = sno;
        self.sname = sname;
        
    def method2(self):
        print("Student No : " ,self.sno);
        print("Student Name : ",self.sname);
       
class marks:
    def setmarks(self, m1,m2):
        self.mark1 = m1;
        self.mark2 = m2;
       
    def putmarks(self):
        print("Mark1 : " , self.mark1);
        print("Mark2 : " , self.mark2);
       
class result(marks,stud): # multiple inheritance
    def calc(self):
        self.total = self.mark1 + self.mark2;
       
    def puttotal(self):
        print("Total : " , self.total);

r =result();
r.method1(60,"Ash");
r.setmarks(50,60);
r.calc();
r.method2();
r.putmarks();
r.puttotal();

Output-

Student No : 60
Student Name : Ash
Mark1 : 50
Mark2 : 60
Total : 110

Multilevel inheritance

* A derived class derived from base class which is again derived from class.

To know details of

A->B->C->D->E
grandfather->father->child

# Example for multilevel inheritance

class stud:
    def setstud(self,sno,sname):
        self.sno = sno;
        self.sname = sname;
        
    def putstud(self):
        print("Student No : " ,self.sno);
        print("Student Name : ",self.sname);
       
class marks(stud):
    def setmarks(self, m1,m2):
        self.mark1 = m1;
        self.mark2 = m2;
       
    def putmarks(self):
        print("Mark1 : " , self.mark1);
        print("Mark2 : " , self.mark2);
       
class result(marks):
    def calc(self):
        self.total = self.mark1 + self.mark2;
       
    def puttotal(self):
        print("Total : " , self.total);

r =result();
r.setstud(60,"Ash");
r.setmarks(50,60);
r.calc();
r.putstud();
r.putmarks();
r.puttotal();

Output-

Student No : 60
Student Name : Ash
Mark1 : 50
Mark2 : 60
Total : 110

Hierarchial inheritance

*A one base class contains more than one derived class.
father->child1 and child2.

#example for hierarchical inheritance

class one:
    def display(self):
        self.x=1000;
        self.y=2000;
        print("This is the method in class one");
        print("Value of X= ",self.x);
        print("Value of Y= ",self.y);

class two (one):
    def add(self):
        print("This is the method in class two");
        print("X+Y= ",(self.x+self.y));

class three(one):
    def mul(self):
        print("This is the method in class three");
        print("X*Y= ",(self.x * self.y));

t1=two();	
t2=three();	
t1.display();
t2.display();
t1.add();		
t2.mul();

Output-

This is the method in class one
Value of X= 1000
Value of Y= 2000
This is the method in class one
Value of X= 1000
Value of Y= 2000
This is the method in class two
X+Y= 3000
This is the method in class three
X*Y= 2000000

Hybird Inheritance

*Combination of multiple + multilevel inheritance.

class stud:
    def setstud(self,sno,sname):
        self.sno = sno;
        self.sname = sname;
        
    def putstud(self):
        print("Student No : " ,self.sno);
        print("Student Name : ",self.sname);
       
class marks(stud):
    def setmarks(self, m1,m2):
        self.mark1 = m1;
        self.mark2 = m2;
       
    def putmarks(self):
        print("Mark1 : " , self.mark1);
        print("Mark2 : " , self.mark2);

class pratical:
    def getpractial(self,p1):
        self.p1=p1;

    def putpractial(self):
        print("Practial mark=",self.p1);
    

       
class result(marks,pratical):
    def calc(self):
        self.total = self.mark1 + self.mark2+self.p1;
       
    def puttotal(self):
        print("Total : " , self.total);

r =result();
r.setstud(60,"Ash");
r.setmarks(50,60);
r.getpractial(100);
r.calc();
r.putstud();
r.putmarks();
r.putpractial()
r.puttotal();

Output-

Student No : 60
Student Name : Ash
Mark1 : 50
Mark2 : 60
Practial mark= 100
Total : 210

One response to “Types of Python Inheritance”

  1. K NARAYANA PRASAD says:

    All types of inheritance are clearly and absolutely explained.
    I really like this

Leave a Reply

Your email address will not be published. Required fields are marked *