Python Simple and Multiple Inheritance

What is inheritance in python?

  • Inheritance is the process by which one object acquires the properties of another object.
  • Inheritance is a mechanism in which one object acquires all the properties and functionalities of parent object.
  • It allows to create the hierarchical classifications.
  • Using inheritance, create a general class (is also called superclass or baseclass) that defines common set of related items. This class can then be inherited by other class(subclass).
  • A subclass is a specialized version of base class.
  • All methods in Python are effectively virtual in default. methods can be overridden in derived classes.

Inheritance syntax in python

class DerivedClass(BaseClass):
    statement1
    .
    .
    .
    statementN
Baseclass should be defined in the same scope containing the derived class definition.

Base class is defined in another module

Need to use module name if base class is defined in separate python file.
class DerivedClass(module_name.BaseClassName):

super() method in python

super method is used to invoke base class properties and methods in python.
super().method_name(arguments);

Example Python Inheritance Program

Create inheritance_test.py file with below python code.
class Parent:                                                                   
    def __init__(self, a, b):                                                   
        self.a = a;                                                             
        self.b = b;                                                             
                                                                                
    def showab(self):                                                           
        print("a and b: %d %d" %  (self.a, self.b));                                                                                                                                                                
               
# Inheritance Child is the subclass and Parent is the base class                                                 
class Child(Parent):                                                            
    def __init__(self, a, b, c):                                                
        super().__init__(a, b);                                                 
        self.c = c;                                                             
                                                                                
    def showc(self):                                                            
        print("c: %d" % self.c);                                                
                                                                                
    def sum(self):                                                              
        print("sum(a+b+c): %d" % (self.a+self.b+self.c));                       
                                                                                
                                                                                
superObj = Parent(20, 30);                                                      
subObj = Child(11, 12, 13);                                                     
superObj.showab();                                                              
                                                                                
subObj.showab();                                                                
subObj.showc();                                                                 
subObj.sum();
Output:
$ python3 inheritance_test.py
a and b: 20 30
a and b: 11 12
c: 13
sum(a+b+c): 36

Multiple Inheritance in python

Python supports a limited form of multiple inheritance.
class DerivedClass(Base1, Base2, Base3):
    statement1
    .
    .
    .
    statementN

Example Multiple Inheritance Python Program

class Parent1:                                                                  
    def __init__(self, a, b):                                                   
        self.a = a;                                                             
        self.b = b;                                                             
                                                                                
    def showab(self):                                                           
        print("a and b: %d %d" %  (self.a, self.b));                                                                                                                                                                
class Parent2:                                                                  
    def __init__(self, c, d):                                                   
        self.c = c;                                                             
        self.d = d;                                                             
                                                                                
    def showcd(self):                                                           
        print("c and d: %d %d" %  (self.c, self.d));                            
     
# multiple inheritance                                                                           
class Child(Parent1, Parent2):                                                  
    def __init__(self, a, b, c, d, e):                                          
        Parent1.__init__(self, a, b);                                           
        Parent2.__init__(self, c, d);                                           
        self.e = e;                                                             
                                                                                
    def showc(self):                                                            
        print("e: %d" % self.e);                                                
                                                                                
    def sum(self):                                                              
        print("sum(a+b+c+d+e): %d" % (self.a+self.b+self.c+self.d+self.e));     
                                                                                
                                                                                
superObj1 = Parent1(20, 30);                                                    
superObj2 = Parent2(40, 50);                                                    
subObj = Child(11, 12, 13, 14, 15);                                             
superObj1.showab();                                                             
superObj2.showcd();                                                             
                                                                                
subObj.showab();                                                                
subObj.showcd();                                                                
subObj.showc();                                                                 
subObj.sum();   
Output:
$ python3 inheritance_test1.py 
a and b: 20 30
c and d: 40 50
a and b: 11 12
c and d: 13 14
e: 15
sum(a+b+c+d+e): 65

Two built-in functions for inheritance in python

  • isinstance() method is used to check an instance’s type.
     isinstance(obj, class1) 
    
    True only if obj.__class__ is class1 or some class derived from class1.
  • issubclass() method is used to check class inheritance.
    issubclass(class1, class2) 
    
    True since class1 is a subclass of class2.
    issubclass(class1, class2) 
    
    False since class1 is not a subclass of class2.
class Parent1:                                                                  
    def __init__(self, a, b):                                                   
        self.a = a;                                                             
        self.b = b;                                                             
                                                                                
    def showab(self):                                                           
        print("a and b: %d %d" %  (self.a, self.b));                                                                                                                                                                
class Parent2:                                                                  
    def __init__(self, c, d):                                                   
        self.c = c;                                                             
        self.d = d;                                                             
                                                                                
    def showcd(self):                                                           
        print("c and d: %d %d" %  (self.c, self.d));                            
                                                                                
class Child(Parent1, Parent2):                                                  
    def __init__(self, a, b, c, d, e):                                          
        Parent1.__init__(self, a, b);                                           
        Parent2.__init__(self, c, d);                                           
        self.e = e;                                                             
                                                                                
    def showc(self):                                                            
        print("e: %d" % self.e);                                                
                                                                                
    def sum(self):                                                              
        print("sum(a+b+c+d+e): %d" % (self.a+self.b+self.c+self.d+self.e));     
                                                                                
                                                                                
superObj1 = Parent1(20, 30);                                                    
superObj2 = Parent2(40, 50);                                                    
subObj = Child(11, 12, 13, 14, 15);                                             
                                                                                
if isinstance(superObj1, Parent1):                                              
    print("superObj1 is instance of Class Parent1");                           
                                                                                
if issubclass(Child, Parent1):                                                  
    print("Child class is sub class of Class Parent1");                        
                                                                                
if issubclass(Child, Parent2):                                                  
    print("Child class is sub class of Class Parent2");   
Output:
$ python3 inheritance_test1.py 
superObj1 is instance of Class Parent1
Child class is sub class of Class Parent1
Child class is sub class of Class Parent2

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^