Python multiple inheritance

What is multiple inheritance
When a child class extends more than one class or it has more than one parent class, then the type of inheritance is called multiple inheritance. Many programming languages do not support this type of inheritance but python supports it.
Following illustration will clarify the concept of multiple inheritance.
multiple inheritance conceptwhere a class can have more than one parent classes.

Before moving on with this post, it is recommended to understand the concept of inheritance in python, if you are not already familiar with it.

Python multiple inheritance syntax
In order for a class to extend more than one classes, all its parent class names are written after the name of this class enclosed between parenthesis and separated by a comma.
Python syntax for a class to extend more than one class is given below.

class ParentOne:
# class body

class ParentTwo:
# class body

class Child(ParentOne, ParentTwo):
# class body

Example

class Father:

    # parent class constructor
    def __init__(self, name):
        self.fname = name

    
class Mother:

    # parent class constructor
    def __init__(self, name):
        self.mname = name


# child class with two parent classes
class Child(Father, Mother):

    # child class constructor
    def __init__(self, father_name, mother_name, name):
        # call parent class constructor
        Father.__init__(self,father_name)
        # call parent class constructor
        Mother.__init__(self,mother_name)
        # create instance variable for this class
        self.name = name

    def printFamilyName(self):
        # access a parent class variable
        print("Father's name is " + self.fname)
        # access another parent class variable
        print("Mother's name is " + self.mname)
        print("Child's name is "+self.name)

# create child class object
c = Child("John", "Elizabeth", "James")
# call its method
c.printFamilyName()

Above code contains three classes where one class extends the other two classes. Its constructor calls the constructor of both its parent classes to initialize their instance variables. It also contains a method which prints the values of instance variables of both its parents.
An object of child class is created and its method is called resulting in below output

Father’s name is John
Mother’s name is Elizabeth
Child’s name is James

Diamond problem
The reason why many programming languages do not support multiple inheritance is name resolution conflict. Suppose a class C extends classes A and B both containing a field or method with same name. When an object of child class is created and common method is called, there rises an ambiguity as to which parent class method should be called. This situation is also referred to as Diamond problem. A code example will be beneficial here.

class Father:

    # parent class constructor
    def __init__(self, name):
        self.name = name

    # parent class method
    def print_name(self):
        print("Father's name is " + self.name)

class Mother:

    # parent class constructor
    def __init__(self, name):
        self.name = name
    
    # parent class method
    def print_name(self):
        print("Mother's name is " + self.name)

class Child(Father, Mother):

    # child class constructor
    def __init__(self, father_name, mother_name, name):
        Mother.__init__(self,mother_name)
        Father.__init__(self,father_name)

# create child class object
c = Child("John", "Elizabeth", "James")
# call its method
c.print_name()

Child class has two parent classes, each of which contains a method with same name. Child object has access to both these methods. Now when child object is created and it attempts to call this method, which method should be called.

Python has handled the solution to Diamond problem beautifully. If a class extends more than one class and if those classes have a method with the same name and when the child class object attempts to access the common method, then the method of the first extended class will be called. If the first extended class does not have a method with this name, then it will call the method in the second class and so on. Thus, the order of search will move from left to right.
That is,
If the class C extends classes A and B in order A, B then method of class A will be called.
If the class C extends classes A and B in order B, A then method or field of class B will be called.
Thus, in above example, following value will be printed.

Father’s name is John

Note that method of class Father is called since it was extended first and it contains called method.

If all the parent classes and the child class itself do not have a method matching the name of method which is called, then an error will be raised

There is one more thing to note here. If the parent classes contain a field with same name, then the value which is set last is used as happened in the above example. Both parent classes contain name field which are initialized from constructor of Child class. Last initialized value is printed. Try reversing the order of initialization of fields in Child constructor and notice the difference.
Hope this post clarified the concept of multiple inheritance in python.

Leave a Reply