Multilevel Inheritance
Multilevel Inheritance
Section titled “Multilevel Inheritance”Definition
Section titled “Definition”Multilevel inheritance is a chain of inheritance where class C inherits from class B, which inherits from class A. Each level in the chain adds more specific behavior.
Characteristics
Section titled “Characteristics”| Aspect | Description |
|---|---|
| Structure | Grandparent → Parent → Child |
| Complexity | Medium — must understand the full chain |
| Risk | Fragile base class problem |
| Depth Limit | Practical limit: 3-5 levels |
Example
Section titled “Example”class Animal { constructor(name) { this.name = name; } eat() { console.log(`${this.name} is eating`); }}
class Mammal extends Animal { constructor(name, hasFur = true) { super(name); this.hasFur = hasFur; } breathe() { console.log(`${this.name} is breathing`); }}
class Dog extends Mammal { constructor(name, breed) { super(name); this.breed = breed; } bark() { console.log(`${this.name} barks!`); }}
const dog = new Dog("Rex", "Labrador");dog.eat(); // Inherited from Animaldog.breathe(); // Inherited from Mammaldog.bark(); // Own methodVisual Diagram
Section titled “Visual Diagram”classDiagram class Animal { +eat() void }
class Mammal { +breathe() void }
class Dog { +bark() void }
Animal <|-- Mammal : extends Mammal <|-- Dog : extendsPrevious Topic: Single Inheritance → Next Topic: Hierarchical Inheritance →