Key Idea: Object-oriented programming builds a program from objects that hold their own data and the operations on it. That is what lets an object enforce its own rules, however the rest of the program uses it.
Paper 1
- Define the four principles; identify a relationship.
Paper 2
- Design classes for a scenario and justify the design.
Both
- The justification carries the marks — say what the alternative would break.
🗝️ The vocabulary
| Term | Meaning |
|---|---|
| Class | The definition — what attributes and methods objects of that kind have |
| Object | One instance, with its own values for those attributes |
| Attribute | A piece of data an object holds |
| Method | An operation an object can perform |
| Constructor | Runs when an object is created, setting it to a valid state |
| Instantiation | Creating an object from a class |
🏛️ The four principles
Learn the one-line definitions
- Encapsulation — data kept private, reachable only through the class's own methods
- Inheritance — one class defined as a specialised version of another, taking on its members
- Polymorphism — objects of different classes responding to the same call in their own ways
- Abstraction — exposing only the operations a user needs and hiding how they work
A Car HAS an Engine — that is association. A SavingsAccount IS a BankAccount — that is inheritance. Say the sentence aloud; if 'is a' sounds absurd, inheritance is the wrong tool.
⚙️ Static against non-static
| Static | Non-static | |
|---|---|---|
| Belongs to | The class | Each object |
| Copies | One, shared by every object | One per object |
| Called | Without any object | On a particular object |
| Can use instance attributes | No | Yes |
🔗 References and copies
Where this catches people out
- A variable holds a reference to an object, not the object itself
- Assigning it copies the reference — both variables then refer to one object
- A shallow copy duplicates the attributes as they stand, so nested objects stay shared
- A deep copy duplicates those too, so the objects share nothing
- A null reference points at nothing; calling through it fails where the call is, not where the mistake was
Important: Returning a reference to a private list hands callers the real object — they can modify it directly, bypassing every rule the class enforces. Return a copy.
📝 Exam-style questions
A class Account keeps its balance private and offers deposit() and withdraw(). A developer proposes adding a public setBalance() method. Explain why this is a poor design decision.
🔒 Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.
A gym has members and staff. Both have a name, an ID and a joining date. Members have a membership type; staff have a role and a salary. Construct a class design, justifying the use of inheritance.
🔒 Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.
✅ Quick check
Cover the answers.
Class or object — which is the definition? The class. An object is one instance created from it, with its own attribute values.
A Car and an Engine — inheritance or association? Association — a Car HAS an Engine. 'A Car is an Engine' is absurd, so inheritance is wrong.
What does a constructor guarantee? That an object starts in a valid state — its attributes are set before anything can use it.
Two variables are assigned the same object. One is modified. What does the other see? The change — both hold a reference to the same object, not to copies.
Why is 'private' not a security measure? It manages dependencies, not access to memory — the data sits there unencrypted and a debugger can read it.
Exam tips
- Use the is-a test aloud before claiming inheritance.
- Design questions want the JUSTIFICATION — say what the alternative would break.
- Encapsulation: name the rule that a public setter would bypass.
- Assignment copies the reference, not the object. Say so explicitly in trace questions.
- A shallow copy leaves nested objects shared; a deep copy does not.
- Static belongs to the class, non-static to the object. One copy against one per object.