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

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