Questions And Answers

More Tutorials

Python Multiple Inheritance

Single inheritance exists when a class is only derived from a single base class. Or in other words, when a child class uses the methods and properties of only a single parent class, then single inheritance is exhibited.

"In multiple inheritance, a class is derived from more than one class i.e. multiple base classes. The child class, in this case, has features of both the parent classes."

As the name implies, python's multiple inheritance is when a class inherits from more than one class. This concept is very similar to multilevel inheritance, which also is our next topic of this course. It is also nearly the same as a single-level inheritance because it contains all of the same functionality, except for the number of base classes.

While using the concept of multiple inheritance, the order of placing the base classes is very important. Let us clear the concept using an example. Suppose we have a child class named Child, and it has two base classes, named Base1 and Base2.


class Base1:
      def func1(self):
            print("this is Base1 class")
class Base2:
      def func2(self):
            print("this is Base2 class")

class Child(Base1 , Base2):
      def func3(self):
            print("this is Base3 class")

obj = Child()


this is Base1 class

this is Base2 class

this is Base3 class

Now, when we are looking for some attribute, let it be a constructor. Then the program will search the current class i.e., the Child1 class first. If it does not find it in the Child1, it will look in the base class that is present at the leftmost side, which is Base1. After that, the program will start moving from left to right in a sequential manner, hence searching the Base2 class at the end. We should always give attention to the ordering of the base classes because it helps us a lot when multiple classes contain the same methods and also in method overriding.

Method Overriding:

Override means having two methods that have the same name. They may perform same tasks or different tasks. In python, when the same method defined in the parent class is also defined in the child class, the process is known as Method overriding. This is also true when multiple classes have the same method and are linked together somehow.

There are few rules for Method overriding that should be followed:

The name of the child method should be the same as parents.
Inheritance should be there, and we need to derive a child class from a parent class
Both of their parameters should be the same.

In this case, the child method will run, and the reason for which, we have discussed in the paragraph above, related to ordering. Multiple inheritance is based on the same concept on which the single inheritance is based on i.e., DRY (do not repeat yourself). Multiple inheritance makes it easier to inherit methods and attributes from base classes that implement the functionality. When done right, we can reuse the code without having to copy-and-paste a similar code to implement interfaces.


class Employee:
    no_of_leaves = 8
    var = 8

    def __init__(self, aname, asalary, arole): = aname
        self.salary = asalary
        self.role = arole

    def printdetails(self):
        return f"The Name is {}. Salary is {self.salary} and role is {self.role}"

    def change_leaves(cls, newleaves):
        cls.no_of_leaves = newleaves

    def from_dash(cls, string):
        return cls(*string.split("-"))

    def printgood(string):
        print("This is good " + string)

class Player:
    var = 9
    no_of_games = 4
    def __init__(self, name, game): = name =game

    def printdetails(self):
        return f"The Name is {}. Game is {}"

class CoolProgramer(Player, Employee):

    language = "C++"
    def printlanguage(self):

harry = Employee("Harry", 255, "Instructor")
rohan = Employee("Rohan", 455, "Student")

shubham = Player("Shubham", ["Cricket"])
karan = CoolProgramer("Karan",["Cricket"])
# det = karan.printdetails()
# karan.printlanguage()
# print(det)


In this page (written and validated by ) you learned about Python Multiple Inheritance . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Multilevel Inheritance.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.