Composition Over Inheritance
Prefer has-a designs where they fit.
Composition Over Inheritance is a free Dart 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 Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Ways to Reuse
You can reuse behavior by inheriting it or by holding another object. The second approach is called composition.
is-a vs has-a
Inheritance models is-a: a Circle is a Shape. Composition models has-a: a Car has an Engine.
A Composed Class
With composition you store a helper as a field and delegate work to it instead of extending it.
class Car {
final Engine engine = Engine();
}Delegating Behavior
The outer class forwards calls to its inner object. This delegation reuses logic without any inheritance.
class Car {
final engine = Engine();
void start() => engine.ignite();
}Fragile Base Class Problem
Deep inheritance is brittle. A change in a parent can silently break far-off subclasses, a trap known as the fragile base class.
Flexibility at Runtime
Composition lets you swap the inner object whenever you like. Inheritance is fixed once, but a field can change.
class Car {
Engine engine;
Car(this.engine);
}Combine With Interfaces
Pair composition with interfaces: depend on an abstract Engine type so any matching engine plugs in cleanly.
abstract class Engine {
void ignite();
}Easier to Test
Because parts are separate objects, you can pass a fake one in tests. Composition makes mocking simple and natural.
var car = Car(FakeEngine());Avoid Inheriting the Wrong Things
Subclasses inherit every member, even ones they should not expose. Composition shares only what you choose to delegate.
When Inheritance Still Wins
Inheritance is fine for a clear is-a bond with shared identity. Reach for composition when the link is really has-a.
The Practical Rule
Favor composition by default and use inheritance sparingly. This keeps designs loosely coupled and easy to change later. 🧩
Quick Check
One quick question about composition.
Recap
Composition models has-a by holding and delegating to other objects. It stays flexible and testable, while inheritance fits clear is-a bonds. 👍
Frequently asked questions
Is the “Composition Over Inheritance” lesson free?
Yes — the full text of “Composition Over Inheritance” is free to read here on the web, and the Dart 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 Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Composition Over Inheritance”?
Prefer has-a designs where they fit. You practise Dart 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 Dart Academy?
No prior experience is required. Dart 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 Over 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 Dart Academy lesson?
Yes. Every Dart 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
- Abstract Classes and Abstract Methods
- Implicit Interfaces and implements
- Polymorphism Through Shared Types
- Composition Over Inheritance