Skip to content

Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance (e.g., single + multiple, or multilevel + hierarchical). It creates complex inheritance graphs.

⚠️ Rarely used and generally discouraged. Hybrid inheritance often leads to the diamond problem and makes code difficult to understand and maintain.


AspectDescription
StructureMix of multiple inheritance types
ComplexityVery high — hard to trace
Diamond ProblemAlmost always present
SupportC++ (rarely)
RecommendationAvoid — use composition instead

graph TD
A[Class A] --> B[Class B]
A --> C[Class C]
B --> D[Class D]
C --> D
E[Class E] --> D
note["Combines hierarchical (A→B, A→C)<br/>+ multiple (D inherits B and C)<br/>+ single (E→D)"]

ReasonExplanation
ComplexityHard to trace method resolution through the hierarchy
AmbiguityDiamond problem creates method ambiguity
MaintenanceChanges in any parent can break the child
ReadabilityDevelopers must understand the entire graph
TestingDifficult to test in isolation

Instead of hybrid inheritance, use composition:

class Engine {
start() { /* engine logic */ }
}
class Wheels {
rotate() { /* wheel logic */ }
}
class Car {
constructor() {
this.engine = new Engine(); // Composition
this.wheels = new Wheels(); // Composition
}
drive() {
this.engine.start();
this.wheels.rotate();
}
}

Previous Topic: Multiple Inheritance → Next Topic: Super Keyword →