Is a, or has a?
After this: Use the sentence test on six pairs, and name the three signs that composition fits better.
On this page · 4 sections · 2 checks
You are writing a racing game. An Engine class already exists, and it has a start method. Now you need a Car, and a car must start. There are two ways to give Car that method, and both of them run. This tutorial is about choosing between them, and why the choice matters more as the program grows.
Two ways to reuse a class
The first way is to make Car a child of Engine. The child gets every field and method of its parent, so car.start() works at once. This is , which you met in "Objects that share behaviour".
The second way is to give Car a field that holds an Engine object. The car's own start method calls the engine's start and returns what it says. This is : the car is built from parts.
| Inheritance | Composition | |
|---|---|---|
| Say it as | "a car is an engine" | "a car has an engine" |
| In the code | class Car(Engine):class Car extends Engine { | self.engine = enginethis.engine = engine; in the constructor |
| In a class diagram | a hollow triangle at the parent | a diamond at the car's end |
Both run. Only one of the two sentences is true. A car is not an engine: it has one, and a mechanic can take it out and put another in.
The sentence test
You already know the test from the Programming fundamentals path: say the sentence out loud. If "X is a Y" is true, inheritance fits. If "X has a Y" is the true one, use composition. Here it is on six pairs.
| Pair | The true sentence | Link |
|---|---|---|
| Circle and Shape | a circle is a shape | inheritance |
| Car and Engine | a car has an engine | composition |
| Manager and Employee | a manager is an employee | inheritance |
| Order and OrderLine | an order has order lines | composition |
| Stack and list | a stack has a list inside it | composition (lesson 4 shows why) |
| Penguin and Bird | a penguin is a bird... | inheritance, with a catch |
Two rows need a word more. A stack can be built on a list, and it is tempting to inherit from one. Lesson 4 shows what goes wrong. And a penguin is a bird, but if your Bird class has a fly method, the penguin inherits a method it cannot honestly do. The sentence test passes and the design still breaks. A later tutorial, "SOLID, one principle at a time", comes back to this bird.
Why "over" inheritance?
The title of this tutorial is a rule of thumb that designers have repeated for decades: when in doubt, prefer composition. The reason is what happens when the program changes. A parent is fixed when you write the class. A part is an object in a field, so it can be a different object for each car, and it can even be swapped while the program runs.
The rest of the tutorial shows three situations where this matters:
- The kinds multiply. Two kinds of body and three kinds of engine. Lesson 2 counts the classes each design needs.
- The part should change. A car moves from a petrol engine to an electric one. Lesson 3 swaps it in one line.
- The parent gives too much. A stack built on a list inherits methods that break the stack. Lesson 4.
The answer is not "never inherit". When the "is a" sentence is true and the child wants everything the parent does, inheritance is the right tool, and lesson 4 ends with when to keep it.
Check yourself
A program has a Library class and a Book class. Say the two sentences out loud. Which link fits?
A programmer wants a car to start, and an Engine class already has a start method. They write class Car(Engine):class Car extends Engine { so that the car gets start. The program runs. What is wrong with this design?
- Say the difference: inheritance makes a class a kind of its parent; composition builds a class from objects it holds in its fields.
- Use the sentence test: if "X is a Y" is true, inheritance fits; if "X has a Y" is the true one, use composition.
- Name the three signs that composition fits better: the kinds multiply, the part should change, or the parent offers too much.
- Say that a true "is a" can still break, as a penguin that inherits fly shows.