Composition vs Inheritance
Prefer composition for shared behavior.
Composition vs Inheritance is a free Angular Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Reuse Strategies
To share behavior between components you can use inheritance (extend a base class) or composition (combine smaller pieces). Angular's composition API makes composition the preferred default.
Inheritance Recap
With inheritance, a component extends a base component or directive and reuses its inputs, outputs, and logic.
export class FancyButton extends BaseButton {
// inherits BaseButton members
}Inheritance Limitations
A class can extend only one base. Deep hierarchies get rigid, and you inherit everything, even parts you do not want.
The Diamond Problem
Combining multiple behaviors via inheritance is impossible without awkward chains. Composition sidesteps this by applying many directives independently.
Composition Recap
With composition, a component stays focused and pulls in behaviors via hostDirectives, mixing as many as needed.
hostDirectives: [ElevateDirective, RippleDirective, FocusTrapDirective]Favor Composition
The guidance "favor composition over inheritance" applies strongly in Angular: composition is flexible, additive, and avoids fragile base classes.
When Inheritance Still Fits
Inheritance can be fine for a single, stable base shared by close variants, for example a base form-field that variants specialize. Keep hierarchies shallow.
Mixing Both
You can combine approaches: extend a thin base for shared structure while composing behaviors for cross-cutting concerns.
Encapsulation
Composition keeps each behavior's internals private and exposes only chosen inputs/outputs, whereas inheritance exposes the entire base API.
Refactoring Toward Composition
When a base class accumulates unrelated responsibilities, split each into a directive and compose them. The result is more modular and testable.
Decision Guide
Need multiple independent behaviors? Compose. Need a single shared structural foundation? A shallow base may do. Default to composition for flexibility.
Quick Check
Test your understanding of composition vs inheritance.
Recap
You learned that composition (combining focused behaviors via hostDirectives) is more flexible than inheritance (a single base class), avoids fragile hierarchies, and should be the default, with shallow inheritance reserved for shared structure.
Frequently asked questions
Is the “Composition vs Inheritance” lesson free?
Yes — the full text of “Composition vs Inheritance” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.
What will I learn in “Composition vs Inheritance”?
Prefer composition for shared behavior. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Angular Academy?
No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Composition vs Inheritance” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Angular Academy lesson?
Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The hostDirectives Property
- Exposing Inputs and Outputs
- Building Reusable Behaviors
- Composition vs Inheritance