← VCE Software Development U3O1

OOP principle: encapsulation

Bundling an object's data and methods together and restricting access, so private data is only changed through public methods that can check values are valid.

Understanding OOP principle: encapsulation in VCE Software Development U3O1

What it does: The method tests the value first, so only a valid amount can update the private attribute. Calling a public method uses the interface, which keeps the data safe, instead of writing account._balance = 50 directly. Encapsulation keeps an object's data and the methods that use it together, and controls how the data is accessed. A leading underscore is the Python convention for 'private - use the class's methods instead of touching this directly'. The class designer writes the validation in a method. Python will not do it unless that check is coded. A setter should validate first, so an invalid age such as -5 is never stored.

Common mistakes: Without self. this makes a local variable that disappears when __init__ ends. The data is not stored in the object. It should be self._balance = 0. Every method in a class needs self as its first parameter so it can reach the object's private data. It should be def set_mark(self, m):.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 125–141) and word search (Modules 114–141).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which best describes encapsulation?
Bundling an object's data and methods together and controlling access to the dataStoring every variable in one global list so any code can reach it
Matches
account._balance = -500
↓
Changes the private attribute directly, bypassing the public method so no validation happens and an invalid negative balance is stored.
Applying
Meanings
Term
encapsulation
Meaning
Bundling an object's data and methods together and restricting access, so private data is only changed through public methods that can check values are valid.
Practice
class Account:
Testing
Gaps
self.mark = 0
Check Answer
Spots
2 def __init__(self):
3 _balance = 0

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Peregrine Falcon' badge — Operator Ace, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which best describes encapsulation?
A. Bundling an object's data and methods together and controlling access to the data ✓
B. Storing every variable in one global list so any code can reach it
Encapsulation keeps an object's data and the methods that use it together, and controls how the data is accessed.
2. Which attribute name signals a private attribute in Python?
A. self.balance
B. self._balance ✓
A leading underscore is the Python convention for 'private - use the class's methods instead of touching this directly'.
3. How should outside code change a private attribute?
A. Call a public method such as deposit(50) ✓
B. Set it directly, e.g. account._balance = -999
Public methods are the interface. They let the class control and check every change to its data.
4. Why does encapsulation protect data from invalid values?
A. Python automatically rejects any value that is too large
B. A public method can check a value before the attribute is changed ✓
The class designer writes the validation in a method. Python will not do it unless that check is coded.
5. Which setter method uses encapsulation correctly?
A. if age >= 0: self._age = age ✓
B. self._age = age (no check at all)
A setter should validate first, so an invalid age such as -5 is never stored.

Match the Pairs

self._balance = 0 → Creates a private attribute by convention, signalling that outside code should not change the balance directly
def deposit(self, amount): → A public method that forms part of the interface, letting outside code change the data in a controlled way
Encapsulation protects data from invalid values by checking them inside a method before changing a private attribute → if amount > 0: self._balance += amount
Outside code should use the public interface rather than reaching into private data → account.deposit(50)
account._balance = -500 → Changes the private attribute directly, bypassing the public method so no validation happens and an invalid negative balance is stored.
def deposit(self, amount): self._balance += amount → The public method changes the private data without checking that the amount is valid, so it does not protect the data.

Key Term

encapsulation
Bundling an object's data and methods together and restricting access, so private data is only changed through public methods that can check values are valid.

Fill in the Blanks

1. class Student: def setup(self): self._mark = 0
Hint: Type the character placed at the start of an attribute name to show it is private.
2. class Student: def getMark(self): return self.mark
Hint: Type the keyword that lets a public method hand back the value of the data to the caller.
3. def setMark(self, mark): if 0 <= mark <= 100: self.mark = mark
Hint: Type the keyword that checks the value is valid before the attribute is changed.
4. def setAge(self, age): if age >= 0: self.age = age
Hint: Type the comparison operator that stops a negative age from being stored.
5. def deposit(self, amount): if amount > 0: self.balance += amount
Hint: Type the operator that adds the amount to the balance, only after the check passes.

Spot the Error

Question 1
A class Account:
B def __init__(self):
C _balance = 0
D def deposit(self, amt):
E if amt > 0:
F self._balance += amt
Line C — Without self. this makes a local variable that disappears when __init__ ends. The data is not stored in the object. It should be self._balance = 0.
Question 2
A class Student:
B def __init__(self):
C self._mark = 0
D def set_mark(m):
E if 0 <= m <= 100:
F self._mark = m
Line D — Every method in a class needs self as its first parameter so it can reach the object's private data. It should be def set_mark(self, m):.
Question 3
A class Player
B def __init__(self):
C self.__health = 100
D def get_health(self):
E return self.__health
Line A — A class header must end with a colon. It should be class Player:.
Question 4
A class Car:
B def __init__(self)
C self._speed = 0
D def get_speed(self):
E return self._speed
Line B — A method header must end with a colon. It should be def __init__(self):.
Question 5
A Class Thermostat:
B def __init__(self):
C self._temp = 20
D def set_temp(self, t):
E if 10 <= t <= 30:
F self._temp = t
Line A — Python keywords are case sensitive. The keyword must be lowercase: class Thermostat:.

Student with a class code? Join your class →