Multiple Inheritance concept in Python

05 min read

We have discussed the concept of inheritance in the previous article where a class was inherited by another class. In pragmatic complex programming situations, there is an obvious need to inherit more than one class at a time for effective execution. This process of inheriting characteristics from multiple classes to use as one class or one object for further operations on the inherited class is called multiple inheritances. Say there is a program where 4 different classes are defined and you need to access the last three classes at once, we could create a fifth class inheriting the parameters of the last three classes. The object of this class can further be created to use it multiple times with ease.

 We all have played Super Mario game in our childhood. Let, us consider a part of the back end program involved in it and code it in Python using object-oriented multiple inheritance concepts. For our ease, consider Mario can move in only one direction and whenever he eats the mushroom he tends to become big. Let us now see the implementation in Python using multiple inheritance concept:

class Mario():                                                                # define a class and a function to move the Mario. 
      def move(self):                                             
                       print('I am moving')

class mushroom():                                             # Define a class and functions inside to grow the size of Mario when he eats a mushroom
         def eat_mushroom(self):
                          print(' I have become big now')

class BigMario(Mario, mushroom):     # multiple classes are inherited into one class 
         pass                                                                  # pass is a function that you use when you do not want any operations to occur inside a defined class.
play = BigMario()                                             # object created
play.move()                                                          # returns 'I am moving'
play.eat_mushroom()                                      # returns  "I have become big now"

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error