Key Idea: A single class holds its own data and rules. Multiple classes have to relate to each other — and choosing the wrong relationship is the most expensive mistake in object-oriented design, because everything built on it inherits the error.
Paper 1
- Define inheritance, polymorphism, abstraction.
- Classify a relationship; name a pattern.
Paper 2
- Design a hierarchy and justify it.
- Construct code showing polymorphism.
Both
- The justification is the marks — say what the alternative would break.
🧬 Choosing the relationship
| Relationship | Test | Example |
|---|---|---|
| Inheritance | "A is a B" | SavingsAccount is an Account |
| Composition | "A has a B", and owns it | House has Rooms |
| Aggregation | "A has a B", which survives it | Course has Students |
First the is-a test: say the sentence aloud. If it is absurd, inheritance is wrong. Then, for has-a, the survival test: delete the whole — does the part still make sense? Yes is aggregation, no is composition.
Important: Reaching for inheritance because a class happens to have methods you want brings everything else it has too, and couples you to its internals. Composition gives you only what you asked for.
🎭 Polymorphism
What it is, and what it replaces
- The same call behaving differently depending on the object it is made on
- Overriding — child replaces a parent's method, resolved at run time
- Overloading — one name, different parameters, one class, resolved at compile time
- It replaces a type-checking if-chain, repeated everywhere the objects are handled
- A new subclass works with existing loops immediately — no working code is edited
🫥 Abstraction
| Abstraction | Encapsulation | |
|---|---|---|
| Asks | What should be visible? | How is it protected? |
| Is about | Design — the interface | Implementation — access control |
| Achieved with | Abstract classes, interfaces | private, protected, getters |
| In one line | Decides | Enforces |
Its abstract methods have no bodies. An object of it could be asked to run a method that does not exist, so the language refuses. It is a promise about children, not a thing.
🧩 Design patterns
| Pattern | Purpose | What it removes |
|---|---|---|
| Singleton | Exactly one instance | Duplicate, inconsistent instances — at the cost of global state |
| Factory | Decides which subclass to create | Concrete class names scattered everywhere |
| Observer | Subscribers notified on change | The subject knowing who is listening |
| Strategy | Interchangeable algorithms | An if-chain selecting behaviour |
📝 Exam-style questions
A delivery company tracks Vans and Bicycles. Both have an ID, a driver and a current location; a Van also has a load capacity and fuel level, a Bicycle a battery level. Every vehicle reports its status, but each does so differently. Construct a design and justify the relationships used.
🔒 Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.
Explain why an interface is preferred to an abstract class when unrelated classes must all be saveable to disc.
🔒 Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.
✅ Quick check
Cover the answers.
Which two tests decide a relationship? is-a for inheritance, then the survival test for has-a: delete the whole, does the part still make sense?
Overriding or overloading — same class, different parameters? Overloading, resolved at compile time. Overriding is a child replacing a parent's method, at run time.
Abstraction or encapsulation — which decides, which enforces? Abstraction decides what should be visible. Encapsulation enforces it.
What does the observer pattern remove? The subject having to know who is listening — so new observers need no change to it.
Why does polymorphism mean no working code breaks? A new subclass works with existing loops immediately, so nothing that currently works is edited.
Exam tips
- Say the is-a sentence aloud before claiming inheritance.
- Justify with BOTH the relationship and what duplication would cost.
- Show ONE call giving different outputs — that is the evidence for polymorphism.
- Abstraction decides, encapsulation enforces. Never swap them.
- Name what a pattern REMOVES, not just what it is.
- A drawback, honestly given, frequently scores a mark on its own.