When a family grows sideways
After this: Count the subclasses one per pair needs, against one class per part, and read the composed design.
On this page · 5 sections · 3 checks
A shop sells vehicles. It has two bodies, car and truck, and three engines: petrol, electric and hybrid. Any body can have any engine. How many classes does the program need? The answer depends on the design, and the difference grows fast.
One subclass per pair
With inheritance, a petrol car is a kind of vehicle, and so is an electric truck. Each pair of body and engine becomes a subclass of Vehicle:
- Car with each engine:
PetrolCar,ElectricCar,HybridCar. - Truck with each engine:
PetrolTruck,ElectricTruck,HybridTruck.
That is 2 × 3 = 6 subclasses. Each one writes its own cost and its own sound, and the petrol cost is written twice: once in PetrolCar and once in PetrolTruck.
- PetrolCar inherits from Vehicle
- ElectricCar inherits from Vehicle
- HybridCar inherits from Vehicle
- PetrolTruck inherits from Vehicle
- ElectricTruck inherits from Vehicle
- HybridTruck inherits from Vehicle
The family grows sideways. When the shop adds a third body, a bus, it needs three more subclasses, one for each engine. When it adds a fourth engine, every body needs one more. People call this a : the count multiplies.
One class per part
With composition, a vehicle has an engine. The engine is a separate object, held in a field. You write each kind of engine once, and you make a petrol car by putting a petrol engine into a vehicle while the program runs. No class is needed for the pair.
Count again. One class per body and one per engine: 2 + 3 = 5. The pairs are made from these parts, not written as classes.
| Bodies × engines | Inheritance: subclasses | Composition: classes |
|---|---|---|
| 2 × 3 | 2 × 3 = 6 | 2 + 3 = 5 |
| 3 × 4 | 3 × 4 = 12 | 3 + 4 = 7 |
| 3 × 5 (a fifth engine) | 15, so 3 new | 8, so 1 new |
The rule: inheritance multiplies the choices, composition adds them. With small numbers the two are close. With every new body or engine the gap widens, and every new subclass is one more place where a fix has to be made.
The composed design
Here is the design drawn as a class diagram. Press Run it to build it one step at a time.
Press Run it to build the diagram one step at a time.
- classes
- 0
- relations
- 0
Read it from the top:
- There is one
Vehicleclass. It has two fields:nameandengine. Engineis an , the dashed box: it lists what every engine can do, a cost per kilometre and a sound.- Each kind of engine is one class that implements it. A fourth engine is one more box at the bottom, and
Vehicledoes not change. - The hollow diamond on the line from
Vehiclesays the vehicle holds an engine that was made outside and passed in. The1says it holds exactly one.
Where did the bodies go? In this program a car and a truck differ only in their name, so the body is not a class at all: it is the name field. When bodies behave differently, say a truck carries a load, each body becomes a part of its own, and the count is the 2 + 3 = 5 of the table.
Check yourself
The shop now sells 3 bodies (car, truck, bus) and 4 engines (petrol, electric, hybrid, hydrogen). With one subclass for every pair, how many subclasses of Vehicle are there?
Same shop, 3 bodies and 4 engines, but now with composition, where each body is a class and each engine is a class. How many body and engine classes are there?
Still 3 bodies and 4 engines. A fifth engine arrives. How many new classes must be written in each design?
Honesty notes
- Count the subclasses when every pair of two choices gets its own: 2 bodies × 3 engines is 6, and 3 × 4 is 12.
- Count the classes with composition, one per body and one per engine: 2 + 3 is 5, and 3 + 4 is 7.
- Say why a new engine costs one subclass per body with inheritance, and one class with composition.
- Read the composed design: a Vehicle holding one Engine, and one class per kind of engine.