python tutorial - Python Multiple Inheritance | Multiple Inheritance in Python - learn python - python programming




 multiple inheritance
  • A class can be derived from more than one base classes in Python. This is called multiple inheritance.
  • In multiple inheritance, the features of all the base classes are inherited into the derived class.
  • The syntax for multiple inheritance is similar to single inheritance.
  • Python supports multiple inheritance also. You can derive a child class from more than one base (parent) class.

Python Multiple Inheritance Example:

class Base1:
    pass

class Base2:
    pass

class MultiDerived(Base1, Base2):
    pass
  • Here, MultiDerived is derived from classes Base1 and Base2.

Image representation:

 python multiderived class inherits
  • The multiderived class inherits the properties of both class base1 and base2.

Syntax of multiple inheritance:

class DerivedClassName(Base1, Base2, Base3):  
    <statement-1>  
        .  
        .  
        .  
    <statement-N>   
        Or
    class Base1:  
    pass  
  
    class Base2:  
    pass  
  
    class MultiDerived(Base1, Base2):  
    pass 

Example 1: multiple inheritance

class First(object):  
  def __init__(self):  
    super(First, self).__init__()  
    print("first")  
  
class Second(object):  
  def __init__(self):  
    super(Second, self).__init__()  
    print("second")  
  
class Third(Second, First):  
  def __init__(self):  
    super(Third, self).__init__()  
    print("third")  
  
Third();

Output:

first  
second  
third

Related Searches to Multiple Inheritance in Python