Skip to content

Multiple Inheritance

Multiple inheritance allows a child class to inherit from more than one parent class. This is more powerful but also more complex.

⚠️ Not supported in many languages. JavaScript, TypeScript, Java, and C# do NOT support multiple inheritance with classes. Python and C++ do.


AspectDescription
ParentsTwo or more parent classes
ChildrenOne child inherits from multiple parents
ComplexityHigh — especially the diamond problem
SupportPython, C++ (not Java, JS, C#)

The main issue with multiple inheritance is the diamond problem:

graph TD
A[Class A] --> B[Class B]
A --> C[Class C]
B --> D[Class D]
C --> D
D --> Problem["❌ Diamond Problem<br/>Which implementation of method()<br/>from B or C does D inherit?"]

Python uses Method Resolution Order (MRO) — C3 linearization — to solve the diamond problem.

class A:
def method(self):
return "A"
class B(A):
def method(self):
return "B"
class C(A):
def method(self):
return "C"
class D(B, C):
pass
d = D()
print(d.method()) # "B" — follows MRO: D → B → C → A
print(D.__mro__) # Shows the MRO chain

In languages without multiple inheritance, use interfaces to achieve similar capability:

interface Swimmable {
swim(): void;
}
interface Flyable {
fly(): void;
}
class Duck implements Swimmable, Flyable {
swim() { console.log("Swimming"); }
fly() { console.log("Flying"); }
}

Previous Topic: Hierarchical Inheritance → Next Topic: Hybrid Inheritance →