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):
pass

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):
    pass

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!