In object-oriented programming, inheritance allows a class to inherit attributes and methods from another class. Python, being an object-oriented programming language, supports multiple inheritance, which means a class can inherit from multiple base classes.
Why Python Multiple Inheritance?
Multiple inheritance provides flexibility and allows you to create complex class hierarchies by combining features from multiple base classes. It enables you to reuse code and create specialized classes that inherit from different parent classes, each contributing specific behaviour and functionality.
Multiple inheritance in Python refers to the ability of a class to inherit from multiple base classes. This means that a class can inherit attributes and methods from more than one parent class. To implement multiple inheritance in Python, you need to define a class that inherits from multiple classes by listing the base classes in parentheses after the class name.
Syntax for Multiple Inheritance
To implement multiple inheritance in Python, you need to define a class that inherits from multiple base classes. The syntax is as follows:
class DerivedClass(BaseClass1, BaseClass2, ...):
# class definition
In the above syntax, DerivedClass
is the class you are defining, and BaseClass1
, BaseClass2
, and so on, are the base classes from which DerivedClass
inherits.
Method Resolution Order (MRO)
When you have multiple base classes, Python uses a method resolution order (MRO) to determine the order in which the base classes are searched for a particular method or attribute. The MRO is based on a depth-first, left-to-right algorithm called the C3 linearization.
The MRO can be accessed using the mro()
method on the class or by using the __mro__
attribute. For example:
print(DerivedClass.mro()) # Output: [DerivedClass, BaseClass1, BaseClass2, object]
Example: Multiple Inheritance in Python
Let’s consider an example to understand multiple inheritance better. Suppose we have two base classes, Vehicle
and Radio
, and we want to create a derived class Car
that inherits from both Vehicle
and Radio
.
Here’s the code:
class Vehicle:
def __init__(self, brand):
self.brand = brand
def drive(self):
print(f"{self.brand} is driving.")
class Radio:
def play_music(self):
print("Playing music on the radio.")
class Car(Vehicle, Radio):
def __init__(self, brand, color):
Vehicle.__init__(self, brand)
self.color = color
def honk(self):
print(f"{self.brand} car is honking.")
# Create an instance of Car
my_car = Car("Toyota", "Red")
# Access methods from Vehicle class
my_car.drive() # Output: Toyota is driving.
# Access methods from Radio class
my_car.play_music() # Output: Playing music on the radio.
# Access methods from Car class
my_car.honk() # Output: Toyota car is honking.
In the above example, the Vehicle
class represents a generic vehicle with the drive()
method, while the Radio
class represents a radio with the play_music()
method. The Car
class inherits from both Vehicle
and Radio
using multiple inheritance.
The Car
class has its own __init__()
method that initializes the brand and color of the car. It also has its own method honk()
specific to the Car
class.
By inheriting from both Vehicle
and Radio
, the Car
class can access methods from both base classes. It can drive like a vehicle, play music like a radio, and also have its own specialized behavior.
Method Resolution Order (MRO) and super()
In the example above, we explicitly called the __init__()
method of the Vehicle
class using Vehicle.__init__(self, brand)
to initialize the brand attribute. However, it’s often recommended to use the super()
function to call the base class methods.
Here’s an updated version of the Car
class using super()
:
class Car(Vehicle, Radio):
def __init__(self, brand, color):
super().__init__(brand)
self.color = color
def honk(self):
print(f"{self.brand} car is honking.")
The super().__init__(brand)
line calls the __init__()
method of the first base class (Vehicle
) without explicitly mentioning the class name. This is advantageous when dealing with complex class hierarchies, as it automatically follows the MRO and ensures that all the base class methods are called correctly.
Multiple inheritance in Python allows a class to inherit from multiple base classes. It provides flexibility in creating class hierarchies and enables code reuse. Understanding the Method Resolution Order (MRO) and using the super()
function correctly is crucial when working with multiple inheritance.
Multiple inheritance can be a powerful feature, but it can also lead to complex code and potential conflicts if not used carefully. It’s important to thoughtfully design your classes and inheritance hierarchy to avoid confusion and maintain code clarity.