Last Updated: January 3, 2026
At its core, multiple inheritance allows a class to inherit from more than one base class. This means that a derived class can combine functionalities from multiple base classes, leading to more complex and potentially more powerful designs.
For instance, imagine a Bird class and a Fish class. If we create a FlyingFish class that inherits from both, this fish can both swim and fly, showcasing the benefits of multiple inheritance.
In this example, FlyingFish can use both the fly method from the Bird class and the swim method from the Fish class, thereby illustrating the capability of combining behaviors across different types.
When defining a class with multiple inheritance, the syntax is straightforward. You simply specify multiple base classes separated by commas. However, it's essential to pay attention to the order in which base classes are declared, as it can affect the resolution of class members and the behavior of the program.
Here, we have two base classes, Base1 and Base2, both of which define a display method. To avoid ambiguity when calling display, we explicitly specify which base class's method we want to invoke.
One of the most challenging aspects of multiple inheritance is the potential for ambiguity. When two base classes have a method or property with the same name, the derived class may not know which one to inherit. This situation is commonly referred to as the diamond problem.
Consider the following example:
In this structure, class D inherits from both B and C, which in turn both inherit from A. If we attempt to call show() on an instance of D, the compiler will be unable to determine which show() method to invoke, leading to a compilation error.
To resolve this ambiguity, we can use virtual inheritance. By declaring the base class as virtual, we ensure that only one instance of A is shared between B and C.
By declaring A as a virtual base class, D inherits a single instance of A, avoiding the ambiguity issue.
Multiple inheritance can be incredibly useful in various scenarios:
Bird and Fish), you can create a derived class that embodies both behaviors without duplicating code.Loggable mixin can be combined with various classes to add logging functionality.Here's a practical example of a mixin:
In this case, Admin gets the logging functionality from Loggable while also being able to authenticate as a User.
While multiple inheritance is powerful, it’s important to use it judiciously. Here are some best practices to keep in mind: