Programming Companion
Java

Programming technique · B3.1.5

Encapsulation and information hiding

An object should own the rules that protect its state. External code uses a deliberate interface instead of changing the internal representation without checks.

IB DP CS standard B3.1.5: Explain and apply encapsulation and information hiding through private and public access, controlled class members and protection of valid object state.
Python language difference: Python does not enforce Java-style private and public access modifiers. A leading underscore signals internal use; double underscores trigger name mangling. Good encapsulation still depends on a deliberate interface and valid-state rules, not on pretending access is impossible.

The object owns the rule

Caller

Requests an operation

EnergyCell boundary

ruleuse(amount)
checks before changing charge
visibilityget charge
reports without unrestricted mutation
lockinternal charge
state governed by the object
Valid-state rule: charge must never be negative. Every construction and state-changing path must preserve that rule.
EnergyCell
- charge: int
+ EnergyCell(initialCharge: int)+ use(amount: int): boolean+ getCharge(): int
Minus marks private state; plus marks the public interface.
EnergyCell
- __charge: int
+ __init__(initial_charge)+ use(amount): bool+ get_charge(): int
The UML expresses intended visibility. Python implements the classroom boundary with name mangling and controlled methods.

Apply the boundary in Java

Apply the boundary in Python

Trace every path

RequestCheckResultCharge after request
create with 12not negativeobject created12
use(5)positive and enough chargetrue7
use(20)insufficient chargefalse7 — unchanged
use(0)not positivefalse7 — unchanged

Encapsulation and information hiding are related

Encapsulation

Keep state and the behaviour responsible for that state together inside a class boundary.

Information hiding

Limit reliance on the internal representation so callers depend on deliberate operations instead.

Hidden state alone is not enough

An unsafe method can still place internal state into an invalid condition.

Do not create setters automatically

Expose operations that match responsibility rather than unrestricted writes for every attribute.

Security has a boundary

Access control and conventions protect program design; they do not automatically provide encryption or authentication.

Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Library Book

Challenge ID: PC-T21-C01 · Standards: B3.1.5

Create a LibraryBook class with private title and borrowed state. External code must use public borrow, returnBook and getStatus operations rather than changing fields directly. Reject impossible repeated borrow or return requests and prove that rejected requests leave the object in a valid state.

Scaffold available
A LibraryBook object protecting internal state behind borrow, return and status operations.

Product Class

Challenge ID: PC-T21-C02 · Standards: B3.1.5

Create a Product class with private code, name and price fields. The constructor and updatePrice method must preserve the rule that price is greater than zero. Provide a returning summary operation, test accepted and rejected changes and explain why private fields alone would not help if public methods allowed invalid values.

Scaffold available
A Product class with private code, name and price plus controlled public methods.

Temperature Sensor

Challenge ID: PC-T21-C03 · Standards: B3.1.5

Create a TemperatureSensor with one private reading and a documented valid range. Validate the starting value, provide a guarded updateReading command and a read-only getTemperature query. Test both boundaries and prove that a rejected update does not alter the previous valid state.

Scaffold available
A TemperatureSensor object protecting an internal reading through validated public methods.

Password Vault Model

Challenge ID: PC-T21-C04 · Standards: B3.1.5, B2.3.2, B2.3.4

Build a classroom PasswordVault model with one private stored code. Public methods may check an attempt and update the code only when the current code is correct and the replacement satisfies a documented rule. Never return the stored code. Explain that private access supports information hiding inside this program but is not encryption or complete real-world password security.

Scaffold available
A password-vault model with private stored data and controlled checking and update operations.

Shared OOP foundation complete

You have evaluated OOP, designed and instantiated classes, distinguished class-owned and instance-owned members, and applied controlled state boundaries. The remaining OOP topics are higher-level extensions.