Python OOPs🚀, Deep Dive Into Inheritance🕹️ And Their Types - Part Two

In previous part of this series, OOPs in Python Part 1, we discussed basic concepts of OOPs in Python. In this article, we will learn an essential concept of OOPs, i.e., Inheritance in Python, and its types with simple examples. Moreover, we will study Python overriding, issubclass method, and isinstance method.
 

Python Inheritance

 
Inheritance is one of the essential concepts of Python Object-Oriented programming techniques performed to achieve reuse of code. It is a mechanism to inherit new classes from existing classes and inherit their methods and attributes.
 
Let's start with some of the basic concepts.
  
Base class
 
In inheritance, a base class is the parent class or the super class. It can let its child classes inherit its methods and data fields depending their scope.
Derived class
 
A derived class is the child class that inherits methods and properties of the base class and can also have its own methods and properties. It is also called a subclass.
Syntax for Python inheritance
 
class BaseClass:
      Base class data fields and methods (parent class block)
class DerivedClass (BaseClass):
      Derived class data fields and methods (Child class block)
 
Example 
  1. class dog:          #Base class  
  2.     Name = ‘Dommy’  
  3.     Age = 3  
  4.     Print(“My Pet Name is” +Name, “and current Age is:”+Age)  
  5. class My_Pet(dos):  #Derived class  
  6.     Color=’violet and blue’  
  7.     Print(“My pet”,+Name, “color is:”+color)  
Benefits of Python Inheritance
  • It reduces the size of the duplicate code declarations in a program to achieve code reusability
  • It simulates real-world relationships. 
  • It makes the code structured and scalable.
  • Code is easy to maintaned.

Types of Inheritance in Python

 
In Python, there are five types of inheritances available.
  1. Single inheritance
  2. Multiple inheritance
  3. Hybrid inheritance
  4. Hirarchical inheritance
  5. Multilevle inheritance 
We will discuss them one by one.
 
Python OOPs, Deep Dive Into Inheritance And Their Types
 

Single Inheritance

 
Single inheritance is considered as one child class that is derived from only one parent class only.
 
Python OOPs, Deep Dive Into Inheritance And Their Types

Syntax
 
class Base_class:
    <Base class statements>
class DerivedClass(Base_class):
    <Derived class statements>

Example
  1. class parent:    
  2.     def Dog(name):  #Base class  
  3.         name=name    
  4.         print("Dog name is:",name)    
  5.         
  6. class child(parent):  #Derived class  
  7.     def puppy(self,name1):    
  8.         self.name1=name1           
  9.         print("Puppy name is:",name1)    
  10. parent.Dog ('Boxer')    
  11.         
  12. obj=child()  #Create object for our child class  
  13. obj.puppy ('Jimmy')  #To access child class method that can able to access Base class method also     
 
Output
 
 

Multilevel inheritance

 
When one class inherits from another class, each derived class will act as an intermediate base class to the next derived class. There is no limit for extending derived classes.
Python OOPs, Deep Dive Into Inheritance And Their Types
Syntax
 
class Base_class:
    <base class statements>
class derived_class1(Base_class):
    <Derived_class1, Base_class statements >
class derived_class2(derived_class1):
    <Base_class, derived_class1, derived_class2 statements>
 
Example
  1. class class_A:                  #Base class  
  2.     def grandfather(self):  
  3.         gname='Raja'  
  4.         print("Grandfather Name is:",gname)  
  5.   
  6. class class_B(class_A):         #Inteermediate Base class 
  7.     def father(self):  
  8.         fname='jhon'  
  9.         print("Father Name is:",fname)  
  10.   
  11. class class_C(class_B):        #Derived class 
  12.     def child(self):  
  13.         cname='Chinna'  
  14.         print("Child name is:",cname)  
  15.   
  16. C=class_C() #To create object for Derieved class
  17. C.grandfather() #To acces class A method by class C object
  18. C.father()  #To access class B method by class C object
  19. C.child()   
Output
 
Python OOPs, Deep Dive Into Inheritance And Their Types
 

Multiple inheritance

 
Python can support multiple inheritances, when a new derived class inherits features from more than one base classes. For example, a child can get attributes from his father and mother.
Python OOPs, Deep Dive Into Inheritance And Their Types
The syntax of multiple inheritance is given below.
 
Syntax
 
class base_A:
<class suit>
class base_B:
<class suit>
class derivedclass_C(base_A,base_B):
<class suit>
 
Example  
  1. class class_A():  #Base class A  
  2.     def male(self,name,age):    
  3.         self.name=name    
  4.         self.age=age    
  5.         print("The Male Name is: %s and Age is: %d" %(name,age))    
  6.                 
  7. class class_B():  #Base class B  
  8.     def female(self,name,age):    
  9.         self.name=name    
  10.         self.age=age    
  11.         print("The Female Name is: %s and Age is: %d" %(name,age))    
  12.         
  13. class class_C(class_A,class_B):  #Derived class extended by Base classess A and B  
  14.     def human(self,place1,place2):    
  15.         self.place1=place1    
  16.         self.place2=place2   
  17.         print("Humans possible to live in: %s and Age is: %s" % (place1,place2))    
  18.         
  19. obj=class_C()    
  20. obj.male('Chinna',21)#To access class A method by class C object    
  21. obj.female('Bharathi',2)#To access class B method by class C object  
  22. obj.human('Earth','Moon')       
Output
 
 

Hierarchical Inheritance

 
A hierarchical inheritance works well in python like other object-oriented languages. When more than one derived class extended from one base class consider as a hierarchical inheritance.
Python OOPs, Deep Dive Into Inheritance And Their Types
Syntax
 
class class_A:
    <Base class statements>
class class_B(class_A):
    <class_B and class_A statements>
class class_C(class_A):
    <class_C and class_A statements>

Example
  1. class class_A:  #Base class
  2.     def base():  
  3.         print("This is a Base class")  
  4.   
  5.   
  6. class class_B(class_A):#Derived class 
  7.     def derived_1(self):        
  8.         class_A.base()  
  9.         print("This is a Derived class-1 ")  
  10.   
  11.   
  12. class class_C(class_A):  #Derived class
  13.     def derived_2(self):  
  14.         class_A.base()  
  15.         print("This is a Derived class-2")  
  16.   
  17. obj1=class_B()  #Create object1 for class B
  18. obj2=class_C()  #Create object2 for class C
  19.   
  20. obj1.derived_1() #To handle class B and A properties 
  21. obj2.derived_2() #To handle class C and A properties 
Output
 
Python OOPs, Deep Dive Into Inheritance And Their Types
 

Hybrid inheritance

 
Python provides us the hybrid inheritance for handling more than one type of inheritance in a program. A combination of more than one inheritance, multilevel and hierarchical inheritance work together (inherit its properties) in a program called as Hybrid inheritance.
Python OOPs, Deep Dive Into Inheritance And Their Types
Let’s see a syntax for hybrid inheritance.
 
Syntax

class class_A:
    <class-suit>
class class_B(class_A):
    <class-suit>
class class_C(class_B):
    <class-suit>
class class_D(class_B):
    <class-suit>
 
Example
  1. class A:  #Base class
  2.     def summation(a,b):  
  3.         return a+b  
  4.   
  5. class B(A):  #Intermediate base class B extended by class A (Base class)
  6.       
  7.     def subtract(a,b):  
  8.         x=a-b  
  9.         print("The subtract value is:",x)  
  10.         print("Summation value is:",A.summation(20,30))  
  11.           
  12.   
  13. class C(B):  #Derived class C extended by class B
  14.       
  15.     def multiplication(self,a,b):  
  16.         B.subtract(10,20)  
  17.         y=a*b  
  18.         print("Multiplication value is:",y)  
  19.   
  20. class D(B):  #Derived class D extended by clas B
  21.     def divide(self,a,b):  
  22.         B.subtract(50,30)  
  23.         z=a/b  
  24.         print("Divide value is:",z)  
  25.   
  26. obj1=C()  
  27. obj2=D()  
  28. obj1.multiplication(10,20) # To access Class A, B and C
  29. print(" ")  
  30. obj2.divide(30,5)# To access Class A, B and D 
Output
 
Python OOPs, Deep Dive Into Inheritance And Their Types 
 

Python Method overriding

 
In method overriding, a derived class can override a method of a base class, and add more functionality to it. 
 
Limitation of overriding
  • Function overriding does not perform in the same class.
  • It must be in the same number of names and parameters.
Example 1
  1. class parent:    
  2.     def display(self):    
  3.         print("Befor override")  #Before override statment  
  4. class child(parent):    
  5.     def display(self):    
  6.         print("After override")  #After override statement  
  7. d = child()    
  8. d.display()     
Here, we hve created two classes that simulate parent and child functionality. 
 
Then also created two methods itself each that represented the same name such as display().
 
Next, I created an object for the child class that extended from the parent class.
 
When execute the print statement under the display() method, it will override the class parent statement and display that. Given below
 
Output
 
Python OOPs, Deep Dive Into Inheritance And Their Types
 
Example 2 -  Realtime example of overriding
 
Let’s take a look at this simple example to clearly understand method overriding.
  1. class car:  
  2.     def car_color(self):  
  3.         a='gray_color'  
  4.         return a;  
  5. class toyota(car):  
  6.     def car_color(self): #override method of car class  
  7.         b='black_color'  
  8.         return b;  
  9. class audi(car):  
  10.     def car_color(self): #override method of toyota  
  11.         c='blue_color'  
  12.         return c;  
  13.   
  14. c1=car()  
  15. c2=toyota()  
  16. c3=audi()  
  17.   
  18. print("Mostly liked car color is:",c1.car_color())  
  19. print("Toyata car color is:",c2.car_color())  
  20. print("Audi car color is:",c3.car_color()) 
Output
 
Python OOPs, Deep Dive Into Inheritance And Their Types
 

Python issubclass() method

 
The “issubclass()” method used to find the relationship of the specified classes. It will take an object and subclass name in the parameter to return output as True or False. If it returns True, we confirm the given an object is a subclass of the specified object. Otherwise False.
 
Example
  1. class fruit1:  
  2.     def apple(self):  
  3.         a='green_color'  
  4.         return a;  
  5. class fruit2:  
  6.     def lemon(self):  
  7.         b='yello_color'  
  8.         return b;  
  9. class fruit3:  
  10.     def blueberry(self):  
  11.         c='Blue_color'  
  12.         return c;  
  13. class derived(fruit1,fruit2,fruit3):  
  14.     def derived(sself):  
  15.         d='RGB'  
  16.         return d;  
  17.   
  18. d=derived()  
  19. print(issubclass(derived,fruit1))   #Check the derived class issubclass of fruit1?  
  20. print(issubclass(fruit1,fruit2))    #Check the fruit1 issubclass of fruit2?  
  21. print(issubclass(derived,fruit3))   #Check the derived issubclass of fruit3?  
Output
 
Python OOPs, Deep Dive Into Inheritance And Their Types
 

Python isinstance() method

 
Python can be available to check the relationship between the objects and classes by using “isinstance()” method.
 
Example 
  1. class calculation1:  
  2.     def add(self,x,y):  
  3.         return x+y;  
  4. class calculation2:  
  5.     def sub(self,x,y):  
  6.         return x-y;  
  7. class derived(calculation2):  
  8.     def divide(self,x,y):  
  9.         return x/y;  
  10.   
  11. d=derived()  
  12. c=calculation2()  
  13. print(isinstance(d,derived)) #Check the object d isinstance of derived class  
  14.                                
  15. print(isinstance(d,calculation1))#Check the object d isinstance of calculation1 class  
  16.   
  17. print(isinstance(d,calculation2))#Check the object d isinstance of calculation2  
Output
 
Python OOPs, Deep Dive Into Inheritance And Their Types

Summary

 
I hope, you understood very well about the Inheritance and its types. If you are getting any queries while doing above mentioned, feel free to comment below, and stay tuned, we will see the rest of OOPs concepts in the next article.


Similar Articles