The big idea: Encapsulation keeps an object's data private and provides public methods as the only way to reach it.
So every change goes through code that can check it first — instead of any part of the program writing whatever it likes.
Step to 'Encapsulation': direct assignment refused, the public method allowed.
Interactive diagram
Explore the labelled diagram, charts and maps for this topic in full study mode.
Free preview
This is the free notes preview
You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:
- FlashcardsLock in vocabulary and key terms with spaced repetition.
- Practice questionsAnswer exam-style questions and get instant AI marking.
- Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
- Personalised study planA daily plan built around your exam date and weak areas.
Private
- Reachable only from inside the class
- Python marks it with two underscores; Java writes private
- The normal choice for attributes
Public
- Reachable from anywhere
- The normal choice for methods
- This is the class's agreed way of being used
Getters and setters
- A getter returns a private value: getbalance()
- A setter changes one after checking
- A getter with no setter makes a value read-only from outside
Information hiding is the wider idea: Encapsulation is the mechanism — private data, public methods.
Information hiding is the principle: the outside should know what a class does, not how. So the inside can be rewritten without breaking anything that uses it.
Get feedback like a real examiner
Submit your answers and get instant feedback — what you did well, what's missing, and exactly what to write to score full marks.
Rules are enforced at the door
The object is always valid
The inside can change
The cause of a bug is nearby
A getter and setter for everything is not encapsulation: If every private attribute has a setter that changes it with no checks, the data is effectively public with extra typing.
The value is in the checking — and in not providing a setter at all for things the outside has no business changing.
How this is tested — you must say what the private attribute prevents, and what the public method checks. It comes up two ways:
Paper 2 — working with code
- Explain encapsulation and information hiding, 3-5 marks
- Apply private and public to a given class
- Say why an attribute should not be public
Paper 2 — the algorithmic-thinking question
- Reason about access with no code shown
- Explain what could go wrong without it
The classic trap: Defining encapsulation without saying what it prevents. The mark is for the consequence: a balance set to a negative number, a book lent twice — something the checks refuse.
A Thermostat class holds a target temperature that must stay between 5 and 30 degrees. Explain how encapsulation would enforce this, and what could go wrong without it.
Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.