← VCE Applied Computing U1O2

Encapsulation in OOP

Bundling an object's data and the methods that use it into one class, and controlling access so the data is only changed in safe ways through those methods.

Understanding Encapsulation in OOP in VCE Applied Computing U1O2

What it does: The if statement inside the method controls how the data changes, so only safe values are accepted. Outside code calls a method instead of touching the attribute, so the class can be updated later without rewriting every place that uses it. Encapsulation keeps an object's attributes and the methods that work on them together inside a class. Protecting data lets the class control how it changes, so objects stay valid. A single underscore is only a naming convention in Python. It signals the attribute is meant for internal use. Encapsulation makes maintenance easier because outside code depends on the methods, not the internal details.

Common mistakes: The def line is missing its colon. It should be 'def __init__(self, bal):'. The constructor sets up the object's protected data. Every method in a class needs self as its first parameter so it can reach the object's data. It should be 'def add_points(self, n):'.

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 8–88) and word search (Modules 8–124).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which statement best describes encapsulation?
Bundling data and the methods that use it into one classWriting all your code in one long function
Matches
account._balance = -500
↓
Changes the protected attribute directly from outside the class, skipping the method that checks the value, so it breaks encapsulation
Applying
Meanings
Term
encapsulation
Meaning
Bundling an object's data and the methods that use it into one class, and controlling access so the data is only changed in safe ways through those methods.
Practice
class Account:
Testing
Gaps
Account:
Check Answer
Spots
1class Account:
2 def __init__(self, bal)

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

The 'Stag' badge — Forest Legend, 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 statement best describes encapsulation?
A. Bundling data and the methods that use it into one class ✓
B. Writing all your code in one long function
Encapsulation keeps an object's attributes and the methods that work on them together inside a class.
2. Which class design shows better encapsulation?
A. Any code changes account._balance directly to any value
B. A method deposit() changes self._balance after checking the amount ✓
Encapsulation means data is changed through methods that can check the new value, not by random code outside the class.
3. What is a main reason for protecting an attribute from direct change?
A. To stop it being set to invalid values like a negative age ✓
B. To make the program run faster on every computer
Protecting data lets the class control how it changes, so objects stay valid.
4. Which is true about the underscore in self._score?
A. It makes Python lock the attribute so it can never be accessed
B. It is a convention telling programmers to use methods instead of changing it directly ✓
A single underscore is only a naming convention in Python. It signals the attribute is meant for internal use.
5. Which code keeps data and behaviour together in one class?
A. class Pet: with self.name and a speak() method inside it ✓
B. A global name variable with a speak() function outside any class
In encapsulation the attributes (data) and methods (behaviour) live together inside the class.

Match the Pairs

self._balance = 0 → Stores data inside the object with a leading underscore to show it is protected and should not be changed directly from outside
def deposit(self, amount): → A method in the same class that works on the object's data, keeping data and behaviour bundled together
Protecting data: a method only accepts a new value if it passes a check, so the data cannot be set to something invalid → if amount > 0: self._balance += amount
Benefit for maintenance: code outside the class uses a method, so the inside of the class can change without breaking other code → account.get_balance()
account._balance = -500 → Changes the protected attribute directly from outside the class, skipping the method that checks the value, so it breaks encapsulation
class Account: balance = 0 def deposit(acc, amount): → The data is in the class but the deposit function is outside it, so data and methods are not bundled together

Key Term

encapsulation
Bundling an object's data and the methods that use it into one class, and controlling access so the data is only changed in safe ways through those methods.

Fill in the Blanks

1. class Account: def __init__(self, balance): self._balance = balance
Hint: Type the keyword that bundles an object's data and methods together into one unit.
2. def balance(self): return self._balance
Hint: Type the keyword that hands the stored value back to the code that called the method.
3. def deposit(self, amount): if amount > 0: self._balance += amount
Hint: Type the keyword that makes the method check the amount first, so the data is only changed in a safe way.
4. def withdraw(self, amount): if amount <= self._balance: self._balance -= amount
Hint: Type the comparison operator that checks the withdrawal is not more than the stored balance.
5. acc = Account(100) acc.deposit(50) print(acc.get_balance())
Hint: Type the method name used to add money through the class instead of changing the data directly.

Spot the Error

Question 1
A class Account:
B def __init__(self, bal)
C self._balance = bal
D def get_balance(self):
E return self._balance
Line B — The def line is missing its colon. It should be 'def __init__(self, bal):'. The constructor sets up the object's protected data.
Question 2
A class Player:
B def __init__(self):
C self._score = 0
D def add_points(n):
E self._score += n
Line D — Every method in a class needs self as its first parameter so it can reach the object's data. It should be 'def add_points(self, n):'.
Question 3
A Class Student:
B def __init__(self, name):
C self._name = name
D def get_name(self):
E return self._name
Line A — The keyword class must be all lowercase in Python. It should be 'class Student:'. This line bundles the data and methods together.
Question 4
A class Wallet:
B def __init__(self):
C _cash = 0
D def deposit(self, amt):
E if amt > 0:
F self._cash += amt
Line C — Without 'self.' this makes a local variable, not an attribute stored in the object. It should be 'self._cash = 0'.
Question 5
A class Pet:
B def _init_(self, name):
C self._name = name
D def get_name(self):
E return self._name
Line B — The constructor needs two underscores on each side: __init__. With one underscore Python will not run it when an object is created.

Student with a class code? Join your class →