Revision Notes
OxfordAQA A-Level Computer Science (9645): Encapsulation – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.9.2: encapsulation, public, private and protected access, Python naming, getters and setters, and a self-test.
- Subject
- Computer Science
- Level
- A LEVEL
- Topic
- Topic 9 – Object-oriented and additional programming
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to OxfordAQA A Level Computer Science (9645), 2024-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): OxfordAQA A Level Computer Science.
Syllabus points this page covers
9645 (A Level)
- 3.9.2 Encapsulation
This page covers 1 of the 4 subtopics in topic 9, Object-oriented and additional programming.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (9645).
These notes condense sub-topic 3.9.2 Encapsulation of OxfordAQA International AS and A-level Computer Science (9645), Version 1.1 (A-level content in topic 3.9 Object-oriented and additional programming). For explanations and full examples, see the encapsulation study guide; then practise with the encapsulation practice questions. Code is Python.
What the specification requires
- Know what encapsulation is and how it is used: the ways a class operates and represents data are hidden from other classes.
- Design classes to use encapsulation appropriately.
- Use public, private and protected access modifiers (also called access specifiers) to decide which classes can access properties and methods.
- If using Python, know the naming conventions for public, private and protected.
- Use getter and setter methods for controlled access to data in a class.
Key terms
| Term | Meaning |
|---|---|
| Encapsulation | Bundling data with its methods and hiding the internal details from other classes |
| Access modifier / specifier | Setting that decides which code may use a property or method |
| Public | Accessible from any code |
| Private | Accessible only within the class |
| Protected | Accessible within the class and its subclasses |
| Getter | Public method that returns the value of an attribute |
| Setter | Public method that sets an attribute, usually after validating the new value |
| Validation | Checking that a value is acceptable before using it |
| Name mangling | Python renaming a __name attribute so outside code cannot use the plain name |
Access summary
| Modifier | Same class | Subclass | Other classes | Python | Class diagram |
|---|---|---|---|---|---|
| Public | Yes | Yes | Yes | name |
+ |
| Protected | Yes | Yes | No | _name |
# |
| Private | Yes | No | No | __name |
− |
In C# and VB.Net the modifiers are written as keywords: public, protected, private (VB.Net: Public, Protected, Private).
Why encapsulate?
- Protects data: it cannot be set to an invalid value, because a setter checks it.
- Hides detail: other classes use the public methods only.
- Allows change: the inside of a class can be rewritten without changing code that uses it.
- Easier to debug: only the class’s own methods can change its data.
- Easier to use: other programmers learn the interface, not the internals.
Pattern: private attribute, getter, setter
class Thermostat:
def __init__(self, room, target):
self.room = room # public
self.__target = 18 # private
self.set_target(target)
def get_target(self):
return self.__target
def set_target(self, value):
if 5 <= value <= 30:
self.__target = value
return True
return False
Thermostat("Lab", 21)gives a target of 21.set_target(45)returnsFalseand leaves the target unchanged.t.__targetfrom outside raises anAttributeError.- No getter or setter at all: the attribute is hidden. A getter only: read-only. A setter with validation: controlled change.
Worked reminder: a class that defends itself
class Wallet:
def __init__(self, amount):
self.__balance = amount
def get_balance(self):
return self.__balance
def spend(self, amount):
if 0 < amount <= self.__balance:
self.__balance = self.__balance - amount
return True
return False
A wallet of 20: spend(30) returns False and the balance stays 20; spend(8) returns True and leaves 12. The balance can never go negative.
Design checklist
- Attributes: private (or protected if a subclass needs them).
- Methods used by other classes: public.
- Helper methods: private.
- Getter for each attribute others need to read.
- Setter only where outside change is allowed, with validation.
- Never expose an attribute that has rules without a setter that applies them.
Hiding how data is stored
A class can store data in a different form from the one it shows. A temperature class might store kelvin internally but offer get_celsius and set_celsius publicly. If the class later changes to store Fahrenheit, only the inside changes. Code that calls the public methods is not affected. This is one of the strongest reasons for hiding the representation: the interface stays stable while the implementation changes.
Read-only, write-only and controlled data
| Design | What outside code can do | Example |
|---|---|---|
| Getter only | Read the value, not change it | A circle’s radius, set once in the constructor |
| Getter and validating setter | Read, and change within the rules | A thermostat’s target temperature (5 to 30) |
| Action methods only | Cause changes through named actions | A wallet changed by spend and add |
| No getter, no setter | Nothing directly | A PIN, checked only by try_open(guess) |
Choose the narrowest access that lets the class do its job.
Spotting broken encapsulation
- A public attribute with rules about its value, such as an age or a balance.
- A setter that stores whatever it is given.
- A method that returns a private list directly, which lets outside code change the list. Return a copy or provide methods such as
count()instead. - A subclass that needs an attribute but is forced to use a public one. Protected is the better choice.
- A class whose users need to know how it stores data before they can use it.
Exam traps
- Half a definition. Encapsulation is bundling and hiding.
- Private versus protected. Subclasses can use protected members, not private.
- Decorative setters. A setter with no check does not protect anything.
- Wrong Python prefix. One underscore is protected, two underscores private.
- Forgetting
self.when using an attribute inside a method. - Diagram symbols. + is public, − is private, # is protected.
Quick self-test
- What is encapsulation?
- Which access modifier allows access only from inside the class?
- Which modifier allows access from the class and its subclasses?
- How do you write a private attribute
speedin Python? - What symbol marks a protected member in a class diagram?
- What is a getter?
- Why might a setter contain an
ifstatement? - How can a class make an attribute read-only?
- Give two benefits of encapsulation.
- What happens if outside code reads
obj.__speed? - State the modifier you would normally give to an attribute and to a method that other classes need.
- What is another name for an access modifier?
Answers
- Bundling data and methods together and hiding how the class works and stores data from other classes.
- Private.
- Protected.
self.__speed.- #.
- A public method that returns the value of an attribute.
- To validate the new value so the attribute never holds an invalid value.
- Provide a getter but no setter.
- Any two from: protects data from invalid values, hides detail, internals can change without affecting other code, easier debugging, simpler to use.
- In Python an
AttributeErroris raised, because the name was mangled inside the class. - Private (or protected) for the attribute, and public for the method.
- Access specifier.
- Encapsulation study guide
- Encapsulation practice questions
- Relationships between classes revision notes
Official syllabus
OxfordAQA International AS and A-level Computer Science (9645) specification, Version 1.1, for International AS exams May/June 2025 onwards and International A-level exams May/June 2026 onwards: sub-topic 3.9.2 Encapsulation.
Related resources
-
Study guides
OxfordAQA A-Level Computer Science (9645): Relationships between classes
OxfordAQA 9645 sub-topic 3.9.3 in depth: inheritance, base and subclasses, protected access, overriding, association and class diagrams, with run examples.
Computer Science · OxfordAQA · A Level
Read guide -
Practice questions
OxfordAQA A-Level Computer Science (9645): Relationships between classes – Practice Questions
Eleven original OxfordAQA 9645 questions on sub-topic 3.9.3: inheritance, protected access, overriding, association and class diagrams, with full answers.
Computer Science · OxfordAQA · A Level
Try questions -
Revision notes
OxfordAQA A-Level Computer Science (9645): Relationships between classes – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.9.3: inheritance, base and subclasses, protected access, overriding, association, class diagrams and a self-test.
Computer Science · OxfordAQA · A Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.