IB Computer Science HL · Year 2 · Lesson 13

Encapsulation & Information Hiding

Control access to object state so a class protects its own rules and exposes a clear public interface.

Paper 250 minutesB3.1.5
Targets

Keep valid state inside the class

2 / 9
  • 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.
B3.1.5
Python noteThe official Python specimen uses a leading underscore to identify a private attribute. In Python this is a convention for intended access rather than Java-style enforced privacy.
Learn + retrieve

Rapid Recall Deck

3 / 9

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”.

B3.1.5 · Explain

A class should protect its own rules

4 / 9
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 += amount

Identify 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?

State the rule being protected.
Explain the consequence of uncontrolled access.

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.

B3.1.5 · Apply

Use methods to preserve a valid object state

5 / 9
RequirementA game character has a private health value. Health must start between 0 and 100. Damage may reduce health but must never make it negative.

Construct controlled state changes

Constructor: initialize _health to a valid value.
Getter: return the current health.
Method: 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)
Trace + reason

Private intent only helps when the interface is respected

6 / 9

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.

Exam lens

Teacher-created Paper 2 practice

7 / 9

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.

  1. Benefits can include restricting outside access, preventing accidental/unintended changes, protecting integrity/security, and forcing use of a controlled interface.
  2. def set_celsius(self, value):
        if value >= -273.15:
            self._celsius = value
  3. The internal attribute is not intended for direct outside modification; the public method controls the allowed state change by validating input.
Exit check

Can you explain the design principle?

8 / 9

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.

Homework

Finish the learning cycle

9 / 9

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.