Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
What Is Hybrid Inheritance In Python?
Next article icon

Inheritance is the mechanism to achieve the re-usability of code as one class(child class) can derive the properties of another class(parent class). It also provides transitivity ie. if class C inherits from P then all the sub-classes of C would also inherit from P.
 

Multiple Inheritance 
When a class is derived from more than one base class it is called multiple Inheritance. The derived class inherits all the features of the base case.
 

Multiple Inheritance


 

Syntax:Class Base1:       Body of the classClass Base2:     Body of the classClass Derived(Base1, Base2):     Body of the class


In the coming section, we will see the problem faced during multiple inheritance and how to tackle it with the help of examples.
 

The Diamond Problem
 

Diamond Probem


It refers to an ambiguity that arises when two classes Class2 and Class3 inherit from a superclass Class1 and class Class4 inherits from both Class2 and Class3. If there is a method"m" which is an overridden method in one of Class2 and Class3 or both then the ambiguity arises which of the method "m" Class4 should inherit.
 

When the method is overridden in both classes
 

Python3
# Python Program to depict multiple inheritance# when method is overridden in both classesclassClass1:defm(self):print("In Class1")classClass2(Class1):defm(self):print("In Class2")classClass3(Class1):defm(self):print("In Class3")classClass4(Class2,Class3):passobj=Class4()obj.m()

Output:

In Class2


Note: When you call obj.m() (m on the instance of Class4) the output is In Class2. If Class4 is declared as Class4(Class3, Class2) then the output of obj.m() will be In Class3.
 

When the method is overridden in one of the classes
 

Python3
# Python Program to depict multiple inheritance# when method is overridden in one of the classesclassClass1:defm(self):print("In Class1")classClass2(Class1):passclassClass3(Class1):defm(self):print("In Class3")classClass4(Class2,Class3):passobj=Class4()obj.m()

Output:

In Class3


When every class defines the same method

Python3
# Python Program to depict multiple inheritance# when every class defines the same methodclassClass1:defm(self):print("In Class1")classClass2(Class1):defm(self):print("In Class2")classClass3(Class1):defm(self):print("In Class3")classClass4(Class2,Class3):defm(self):print("In Class4")obj=Class4()obj.m()Class2.m(obj)Class3.m(obj)Class1.m(obj)

Output:

In Class4In Class2In Class3In Class1


The output of the method obj.m() in the above code isIn Class4. The method "m" of Class4 is executed. To execute the method "m" of the other classes it can be done using the class names.
Now, to call the method m for Class1, Class2, Class3 directly from the method "m" of the Class4 see the below example
 

Python3
# Python Program to depict multiple inheritance# when we try to call the method m for Class1,# Class2, Class3 from the method m of Class4classClass1:defm(self):print("In Class1")classClass2(Class1):defm(self):print("In Class2")classClass3(Class1):defm(self):print("In Class3")classClass4(Class2,Class3):defm(self):print("In Class4")Class2.m(self)Class3.m(self)Class1.m(self)obj=Class4()obj.m()

Output:

In Class4In Class2In Class3In Class1


To call "m" of Class1 from both "m" of Class2 and "m" of Class3 instead of Class4 is shown below:

Python3
# Python Program to depict multiple inheritance# when we try to call m of Class1 from both m of# Class2 and m of Class3classClass1:defm(self):print("In Class1")classClass2(Class1):defm(self):print("In Class2")Class1.m(self)classClass3(Class1):defm(self):print("In Class3")Class1.m(self)classClass4(Class2,Class3):defm(self):print("In Class4")Class2.m(self)Class3.m(self)obj=Class4()obj.m()

Output:

In Class4In Class2In Class1In Class3In Class1


The output of the above code has one problem associated with it, the method m of Class1 is called twice. Python provides a solution to the above problem with the help of the super() function. Let’s see how it works.
 

The super Function 

Python3
# Python program to demonstrate# super()classClass1:defm(self):print("In Class1")classClass2(Class1):defm(self):print("In Class2")super().m()classClass3(Class1):defm(self):print("In Class3")super().m()classClass4(Class2,Class3):defm(self):print("In Class4")super().m()obj=Class4()obj.m()

Output:

In Class4In Class2In Class3In Class1


Super() is generally used with the __init__ function when the instances are initialized. The super function comes to a conclusion, on which method to call with the help of themethod resolution order (MRO).
 

Method resolution order:


In Python, every class whether built-in or user-defined is derived from the object class and all the objects are instances of the class object. Hence, the object class is the base class for all the other classes.
In the case of multiple inheritance, a given attribute is first searched in the current class if it's not found then it's searched in the parent classes. The parent classes are searched in a left-right fashion and each class is searched once.
If we see the above example then the order of search for the attributes will be Derived, Base1, Base2, object. The order that is followed is known as a linearization of the class Derived and this order is found out using a set of rulescalled Method Resolution Order (MRO).
To view the MRO of a class: 
 

  • Use the mro() method, it returns a list 
    Eg. Class4.mro()
  • Use the _mro_ attribute, it returns a tuple 
    Eg. Class4.__mro__ 
     


Example: 

Python3
# Python program to demonstrate# super()classClass1:defm(self):print("In Class1")classClass2(Class1):defm(self):print("In Class2")super().m()classClass3(Class1):defm(self):print("In Class3")super().m()classClass4(Class2,Class3):defm(self):print("In Class4")super().m()print(Class4.mro())#This will print listprint(Class4.__mro__)#This will print tuple

Output:

[<class '__main__.Class4'>, <class '__main__.Class2'>, <class '__main__.Class3'>, <class '__main__.Class1'>, <class 'object'>] 
(<class '__main__.Class4'>, <class '__main__.Class2'>, <class '__main__.Class3'>, <class '__main__.Class1'>, <class 'object'>) 
 


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp