Multiple Inheritance
A class can be a subclass of a few other classes (inherit from multiple other classes).
To inherit from multiple classes, you can write the names of the classes separated by commas (,
):
Code Description
The code defines two parent classes,
First
and Second
, and a child class Child
that inherits from both parent classes. The Child
class does not define any additional attributes or methods and simply inherits the attributes and methods from its parent classes.When
print(instance.first_attribute)
is called, it accesses the first_attribute
inherited from the First
class and prints its value, which is "First". Similarly, when print(instance.second_attribute)
is called, it accesses the second_attribute
.Furthermore, the code calls the
first_method()
and second_method()
on the instance
object, which execute the respective methods defined in the First
and Second
classes, printing the corresponding messages.In summary, inheritance allows the child class (
Child
) to inherit and utilize the attributes and methods defined in its parent classes (First
and Second
), providing code reusability and facilitating the organization of related classes into a hierarchy.
But the multiple inheritance has an order. The priority of the attributes/methods search is going from left to right:
Code Description
Child
class is defined as a subclass of both Second
and First
classes. The order of inheritance in the Child
class is specified as Second, First
.When
print(instance.attribute)
is called, it accesses the attribute
attribute from the Child
class.Since the
Child
class inherits from both Second
and First
, the order of inheritance becomes important. In this case, Second
is specified first in the inheritance list. Therefore, the attribute lookup starts from the Second
class.As a result, the output of
print(instance.attribute)
will be "Second". This means that the attribute
attribute of the Second
class takes precedence over the same attribute in the First
class due to the order of inheritance in the Child
class.
This demonstrates how the order of inheritance affects attribute resolution in cases where the same attribute is defined in multiple parent classes.
Everything was clear?
Course Content
In-Depth Python OOP
In-Depth Python OOP
4. Polymorphism and Abstraction
Multiple Inheritance
A class can be a subclass of a few other classes (inherit from multiple other classes).
To inherit from multiple classes, you can write the names of the classes separated by commas (,
):
Code Description
The code defines two parent classes,
First
and Second
, and a child class Child
that inherits from both parent classes. The Child
class does not define any additional attributes or methods and simply inherits the attributes and methods from its parent classes.When
print(instance.first_attribute)
is called, it accesses the first_attribute
inherited from the First
class and prints its value, which is "First". Similarly, when print(instance.second_attribute)
is called, it accesses the second_attribute
.Furthermore, the code calls the
first_method()
and second_method()
on the instance
object, which execute the respective methods defined in the First
and Second
classes, printing the corresponding messages.In summary, inheritance allows the child class (
Child
) to inherit and utilize the attributes and methods defined in its parent classes (First
and Second
), providing code reusability and facilitating the organization of related classes into a hierarchy.
But the multiple inheritance has an order. The priority of the attributes/methods search is going from left to right:
Code Description
Child
class is defined as a subclass of both Second
and First
classes. The order of inheritance in the Child
class is specified as Second, First
.When
print(instance.attribute)
is called, it accesses the attribute
attribute from the Child
class.Since the
Child
class inherits from both Second
and First
, the order of inheritance becomes important. In this case, Second
is specified first in the inheritance list. Therefore, the attribute lookup starts from the Second
class.As a result, the output of
print(instance.attribute)
will be "Second". This means that the attribute
attribute of the Second
class takes precedence over the same attribute in the First
class due to the order of inheritance in the Child
class.
This demonstrates how the order of inheritance affects attribute resolution in cases where the same attribute is defined in multiple parent classes.
Everything was clear?