Types Of Inheritance In Python

Introduction

 
In this article, I will explain the types of inheritance in Python.
 

Definition

 
In Python, we used to create new classes from existing classes. A class that can inherit the attributes and behavior from another class is called inheritance. The class from which another class has been derived is called the base class. The class which inherits the behavior of the class is called the derived class.
 
Advantages of inheritance
  1. Reusability of code
  2. Code sharing
  3. Consistency of interface

Single inheritance

 
When a derived class inherits only from syntax, the base class is called single inheritance. If it has one base class and one derived class it is called single inheritance.
 
Diagram
 
Types Of Inheritance In Python
 
Syntax
  1. class A: #parent class  
  2.     #some code  
  3.   
  4. class b(A): #child class  
  5.     #some code  
Example
  1. class father:#parent class  
  2.   def father_name(self):  
  3.      print("my father name is selvaraj")  
  4.   
  5. class son(father):#child class  
  6.   pass  
  7.   
  8. obj_father= father()#object for father class.  
  9. obj_son=son()#object for son class.  
  10.   
  11. obj_son.father_name()# you access son class to father class
Output
 
Types Of Inheritance In Python
 

Multilevel inheritance

 
Multiple inheritance is the concept where a subclass inherits properties from multiple base classes. It has one base class and two (or) more derived classes called multilevel inheritance.
 
Diagram
 
Types Of Inheritance In Python
 
Syntax
  1. class A: #parent class  
  2.     #some code  
  3.   
  4. class B(A): #child class  
  5.     #some code  
  6.   
  7. class C(B): #child class  
  8.     #some code  
Example
  1. class g_father:#parent class  
  2.   def g_father_name(self):  
  3.      print("my father name is ramasamy")  
  4.   
  5. class father(g_father):#child class  
  6.   def father_name(self):  
  7.      print("my father name is selvaraj")  
  8.   
  9. class son(father):#child class  
  10.   def son_name(self):  
  11.      print("my name is surya")  
  12.   
  13.   
  14. obj_g_father= father()#object for g_father class.  
  15. obj_father= father()#object for father class.  
  16. obj_son=son()#object for son class.  
  17.   
  18. obj_son.g_father_name()# you access son class to g_father class.  
  19. obj_son.father_name()# you access son class to father class.  
  20. obj_son.son_name()# you access same class
Output
 
Types Of Inheritance In Python
 

Multiple inheritance

 
Multiple inheritance is the concept where a subclass inherits properties from multiple base classes. If it has two base classes and one derived class it is called multilevel inheritance.
 
Diagram
 
Syntax
  1. class A: #parent class  
  2.     #some code  
  3.   
  4. class B(A): #parent class  
  5.     #some code  
  6.   
  7. class C (A, B): #child class  
  8.     #some code  
Example
  1. class father:#parent class  
  2.   def father_name(self):  
  3.      print("my father name is selvaraj")  
  4.   
  5. class mother:#parent class  
  6.   def mother_name(self):  
  7.      print("my father name is jayanthi")  
  8.   
  9. class son(mother,father):#child class  
  10.   pass  
  11.   
  12. obj_father= father()#object for father class.  
  13. obj_mother=mother()#object for mother class.  
  14. obj_son=son()#object for son class.  
  15.   
  16. obj_son.father_name()# you access son class to father class.  
  17. obj_son.mother_name()# you access son class to mother class.  
  18.   
  19.   
  20. #obj_mother.father_name()\\This statement did not work because mother and father are parents. 
Output
 
Types Of Inheritance In Python
 

Conclusion

 
In this article, we have seen types of inheritance in Python. I hope this article was useful to you. Thanks for reading!