Encapsulation & Information Hiding
Control access to object state so a class protects its own rules and exposes a clear public interface.
Keep valid state inside the class
- explain encapsulation and information hiding;
- distinguish public access from private state;
- apply controlled access through class methods;
- explain how limiting access protects object integrity and security.
Rapid Recall Deck
Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.
A class should protect its own rules
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self._balance = balance
def get_balance(self):
return self._balance
def deposit(self, amount):
if amount > 0:
self._balance += amountIdentify the interface
Which member is intended to be private, and which methods provide controlled public access?
_balance is intended to be private. get_balance() exposes read access, while deposit(amount) controls how the value may be changed.
Explain the protection
Why is deposit() safer than allowing any outside code to assign an arbitrary value to the balance?
The method enforces the rule that only positive deposits are accepted. If outside code could freely assign any value, invalid or accidental changes could corrupt the account state.
Use methods to preserve a valid object state
Construct controlled state changes
_health to a valid value.take_damage(amount) reduces health but not below 0.class Character:
def __init__(self, health):
self._health = max(0, min(100, health))
def get_health(self):
return self._health
def take_damage(self, amount):
if amount > 0:
self._health = max(0, self._health - amount)Private intent only helps when the interface is respected
Trace the object
c = Character(80) c.take_damage(25) c.take_damage(70) print(c.get_health())
Predict the final output and explain why the value cannot become negative.
The output is 0. The second damage call would go below zero, but max(0, ...) keeps the stored state valid.
Spot the encapsulation failure
Outside code contains c._health = -500. Explain why this undermines the class design even though Python may still allow the assignment.
It bypasses the class’s validation rules and directly creates invalid state. Code should use the public methods that preserve the invariant.
Teacher-created Paper 2 practice
Paper 2 mini-set
1. Explain two benefits of treating an attribute as private and restricting direct outside access.
2. A Temperature class stores _celsius. Construct a method set_celsius(value) that changes the value only when it is at least −273.15.
3. Explain how the method in part 2 demonstrates encapsulation and information hiding.
- Benefits can include restricting outside access, preventing accidental/unintended changes, protecting integrity/security, and forcing use of a controlled interface.
def set_celsius(self, value): if value >= -273.15: self._celsius = value- The internal attribute is not intended for direct outside modification; the public method controls the allowed state change by validating input.
Can you explain the design principle?
Exit explanation
Complete this sentence accurately: “Encapsulation is more than making variables private because …”
…it groups state with the methods that manage that state and exposes a controlled interface that protects the object’s rules.
Finish the learning cycle
IA — main task
Continue Criterion D development/testing. If your product stores important state, note where validation or controlled updates protect correctness.
Paper 2 — short practice
Redo the Temperature method and explain two consequences of private attributes without revealing the model.