Angular Academy · Lesson

Composition vs Inheritance

Prefer composition for shared behavior.

Lesson 4 of 413 steps

Composition vs Inheritance is a free Angular Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.

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.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
38
Lessons
144

Frequently Asked Questions

Is the “Composition vs Inheritance” lesson free?

Yes — any 3 lessons of the Angular Academy learning path, including “Composition vs Inheritance”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. The hostDirectives Property
  2. Exposing Inputs and Outputs
  3. Building Reusable Behaviors
  4. Composition vs Inheritance
← Back to Angular Academy