In this article we are going to talk about Multiple Inheritance in Python, so first of all what is Multiple Inheritance in Python, if a class extends from more than one class, that is called multiple inheritance.
Python Multiple Inheritance
Multiple inheritance in Python is a feature of object-oriented programming, in multiple inheritance one class can inherit attributes and methods from more than one parent class. This allows for greater flexibility in designing class hierarchies and enables code reuse.
Now let’s look at this image example.
In the above image we have three classes, Class A and Class B are our base class, also we have another class that is Class C, now this Class C extends from Class A and Class B, as we have already said that if a class extends from more than one class, that is called multiple inheritance. in here our C Class extends from two base classes, now the C class can access to all attributes and methods of the Class A and Class B.
Let’s create a practical example for Multiple Inheritance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class A: def print_a(self): print("Iam Class A Method") class B: def print_b(self): print("Iam Class B Method ") class C(A,B): pass c = C() c.print_a() c.print_b() |
In the above example we have two base classes and the third class is extending from these two base class, and if you see in our base classes we have methods, after extending Class C can access to these two methods.
Run the code and this is the result.
Let’s create another example, in this example we want to override a method in to our base classes, also we are going to talk about Method Resolution Order (MRO).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class A: def full_name(self): print("This is my full name - Class A") class B: def full_name(self): print("This is my full name - Class B") class C(B, A): pass c = C() c.full_name() |
You can see that when we are extending our Class C, we have the order of B and A, now it will first check the method in the C class, if it does not find the method in the C class, than it checks the B Class, now it means that the order matters in Python Multiple Inheritance, if we run this code you will see that first we have our B Class method.
FAQs:
What is multiple inheritance in Python?
Multiple inheritance in Python is a functionality in OOP that a class to inherit attributes and methods from more than one parent class. This allows a subclass to inherit features from multiple sources.
What is the Method Resolution Order (MRO) in Python?
The Method Resolution Order (MRO) in Python determines the order in which methods are searched and invoked in a class hierarchy with multiple inheritance. Python uses C3 linearization algorithm to compute the MRO, and it ensures a consistent and predictable method resolution order.
What is the diamond problem in multiple inheritance?
The diamond problem is a common issue in multiple inheritance where ambiguity arises due to the presence of a shared ancestor class with overridden methods. This can lead to conflicts when resolving method calls in subclasses. Python’s Method Resolution Order (MRO) helps to solve diamond problem by providing a specific order in which methods are resolved.
What is multiple inheritance with example?
Here’s an example of multiple inheritance in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Parent1: def method1(self): print("Method 1 from Parent1") class Parent2: def method2(self): print("Method 2 from Parent2") class Child(Parent1, Parent2): pass obj = Child() obj.method1() # Output: Method 1 from Parent1 obj.method2() # Output: Method 2 from Parent2 |
Is inheritance single or multiple in Python?
In Python, inheritance can be both single and multiple. Single inheritance refers to a subclass inheriting from only one parent class, and multiple inheritance refers to a subclass inheriting from more than one parent class. Python supports both types of inheritance.
Can you call super() in a class with multiple inheritance?
Yes, you can use the super() function in a class with multiple inheritance to call methods from parent classes. However, it’s essential to understand the Method Resolution Order (MRO) to determine which superclass method will be called.
What are types of inheritance in Python?
In Python, there are several types of inheritance:
- Single Inheritance: A subclass inherits from only one parent class.
- Multiple Inheritance: A subclass inherits from multiple parent classes.
- Multi-level Inheritance: A subclass inherits from a parent class, which in turn inherits from another parent class.
- Hierarchical Inheritance: Multiple subclasses inherit from a single parent class.
- Hybrid Inheritance: Combination of multiple types of inheritance.
Learn More:
Subscribe and Get Free Video Courses & Articles in your Email