Learn / System Design
Module 08 · System Design · Low-level design: classes, properly
Continue where you left off ×
Has-a beats is-a (composition over inheritance)
Has-a Beats Is-a · Open board
The stack that let 99 in at the bottom, the front desk that holds a list instead, and the car that takes either engine.
Before you start. You need two ideas from earlier in this track. Nouns and verbs (from requirements to classes) turned a spoken requirement into classes and the job each one does. The object that keeps its own rules (encapsulation and invariants) showed an object guarding its own data, so no outside code can break its rules. This lesson asks how one class should reuse another class’s work without giving those rules away.
The idea in 30 seconds
Inheritance means a new class takes everything an old class has, and it keeps every door the parent had, even ones that break the child’s own rules.
Composition means a class holds other objects as parts and shows only the calls it chooses.
Delegation is the held part doing the real work when the holder gets a call.
A fragile base class is a parent that cannot change safely, because its children rely on how it works inside.
Polymorphism means code works with any object that answers the right calls, and composition lets you swap those objects while the program runs.
Say has-a first. Use is-a only when the child really is a kind of the parent in every way the parent promises.
Read the story
Lesson 37 of 73 in System Design
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year , or $19.50 for your first year with LAUNCH50 , ends 1 Nov, 18:30 UTC .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson October 1994: a book with a sentence on page 20 The obvious way: inheritance, a family tree The other way: composition, a parts list What breaks later: the fragile base class The same shape, many parts: polymorphism So never inherit? Terms it explains: inheritance, composition, delegation, fragile base class, polymorphism.
Review System Design
Mark as read Saved in this browser, and to your account when you sign in.