Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Multiple Inheritance | Inheritance
In-Depth Python OOP

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
In this code, inheritance is used to create a class hierarchy and enable the sharing of attributes and methods between classes.

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
The 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.

How to inherit a class from Parent1 and Parent2?

Select the correct answer

Everything was clear?

Section 2. Chapter 3
course content

Course Content

In-Depth Python OOP

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
In this code, inheritance is used to create a class hierarchy and enable the sharing of attributes and methods between classes.

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
The 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.

How to inherit a class from Parent1 and Parent2?

Select the correct answer

Everything was clear?

Section 2. Chapter 3
some-alt