Python Multiple Inheritance

Deriving sub classes from multiple (or more than one) base classes is called 'multiple inheritance'. In this type of inheritance, there will be more than one super class and there may be one or more sub classes. All the members of the super classes are by default available to sub classes and the sub classes in turn can have their own members.The syntax for multiple inheritance is shown in the following statement:

class Subclass(Baseclass1, Baseclass2,...):

The best example for multiple inheritance is that parents producing the children and the children inheriting the qualities of the parents.


multiple inheritance example

Multiple inheritance example

A Python program to implement multiple inheritance using two base classes.


Problems in Multiple Inheritance

If the sub class has a constructor, it overrides the super class constructor and hence the super class constructor is not available to the sub class. But writing constructor is very common to initialize the instance variables.

In multiple inheritance, let's assume that a sub class 'C' is derived from two super classes 'A' and 'B' having their own constructors. Even the sub class 'C' also has its constructor. To derive C from A and B, we write:

class C(A, B):

Also, in class C's constructor, we call the super class super class constructor using super().__init__(). Now, if we create an object of class C, first the class C constructor is called. Then super().__init__() will call the class A's constructor.


effect of class

The effect of class C(A, B)

A Python program to prove that only one class constructor is available to sub class in multiple inheritance.


The output of the program indicates that when class C object is created the C's constructor is called. In class C, we used the statement: super().__init__() that calls the class A's constructor only. Hence, we can access only class A's instance variables and not that of class B.


A Python program to access all the instance variables of both the base classes in multiple inheritance.