What is the difference between Multi-Level and Multiple Inheritance?

Even though I seem to understand the syntax of Multi-Level and Multiple Inheritance in Python , I don’t seem to completely understand how they differ in terms of functionality.

For instance the image below is an example of Multi-Level Inheritance. The Chicken class inherits from the Bird class which in turn inherits from the Animal class. Therefore the Chicken class has access to all the attributes and methods of both the Bird and Animal classes.

For Multiple Inheritance this would be:
class Chicken(Animal, Bird):

Here too as I understand, the Chicken class has access to all the attributes and methods of both the Bird and Animal classes which is similar to Multi-Level Inheritance.

So what makes the two types of inheritance different in terms of functionality?

Any help is appreciated!

There is quite a difference actually. You will typically not use multiple inheritance like you showed, i.e. inheriting two separate classes that inherit from each other. This will cause too many headaches with all the methods and attributes that Bird already inherits from Animal and now your Chicken inheriting these from both! (Python does have Method Resolution Order for cases like these but still, it is messy.)

Multiple inheritance is sometimes useful if you want to add functionality from different types of classes that are related but does not have an inheritance relationship. So for example let’s say you have two classes GraphicalObject and Rezisable , both have different attributes and methods and you create a new class RezizableGraphicalObject that has attributes and methods from both, then you will use multiple inheritance eg:

class GraphicalObject:
    def __init__(self, x, y, width, height)
        self.x = x
        self.y = y
        self.width = width
       self.height = height

class Reziable:
    def resize(self, new_width, new_height):
        self.width = new_width
        self.height = new_height

Now you can declare your ReziableGraphicalObject and get the properties of GraphicalObject and the resize method from Resizable through multiple inheritance:

class ResizableGraphicalObject(GraphicalObject, Resizable):

This makes more sense and hope it is clearer.

1 Like

Yes the functionality of Multiple and Multi-Level Inheritance is clearer to me now. Thank you for your detailed explanation!

The key difference between Multiple and Multilevel Inheritance is that Multiple Inheritance is when a class inherits from many base classes while Multilevel Inheritance is when a class inherits from a derived class making that derived class a base class for a new class.

Allowing multiple inheritence makes the rules about function overloads and virtual dispatch decidedly more tricky, as well as the language implementation around object layouts. These impact language designers/implementors quite a bit, and raise the already high bar to get a language done, stable and adopted.

It is simple to think this way, if class A inherits from multiple classes, then the class A will have the same grandparent class multiple times, this means the code will be complicated and a series of bugs will go

unacknowledged. Personally, I think multiple inheritance has a bad rap, and that a well done system of trait style composition would be really powerful/useful… but there are a lot of ways that it can be implemented badly, and a lot of reasons it’s not a good idea in a language like C++.