Hierarchical Inheritance
Hierarchical Inheritance
Section titled “Hierarchical Inheritance”Definition
Section titled “Definition”Hierarchical inheritance occurs when multiple child classes inherit from the same parent class. The parent provides shared behavior, and each child adds its own specialization.
Characteristics
Section titled “Characteristics”| Aspect | Description |
|---|---|
| Structure | One parent → many children |
| Complexity | Low — children are independent |
| Reuse | High — shared code in parent |
| Benefit | Organizes related types under one concept |
Example
Section titled “Example”class Employee { constructor(name, id) { this.name = name; this.id = id; }
work() { return `${this.name} is working`; }}
class Developer extends Employee { constructor(name, id, language) { super(name, id); this.language = language; }
work() { return `${this.name} is coding in ${this.language}`; }}
class Manager extends Employee { constructor(name, id, teamSize) { super(name, id); this.teamSize = teamSize; }
work() { return `${this.name} is managing ${this.teamSize} people`; }}
class Designer extends Employee { constructor(name, id, tool) { super(name, id); this.tool = tool; }
work() { return `${this.name} is designing in ${this.tool}`; }}Visual Diagram
Section titled “Visual Diagram”classDiagram class Employee { +name: string +id: int +work() string }
class Developer { +language: string +work() string }
class Manager { +teamSize: int +work() string }
class Designer { +tool: string +work() string }
Employee <|-- Developer : extends Employee <|-- Manager : extends Employee <|-- Designer : extendsPrevious Topic: Multilevel Inheritance → Next Topic: Multiple Inheritance →