Encapsulation in OOP

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Encapsulation in OOP

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

encapsulation

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_____ Account: def __init__(self, balance): self._balance = balance
2.
def balance(self): ______ self._balance
3.
def deposit(self, amount): __ amount > 0: self._balance += amount
4.
def withdraw(self, amount): if amount __ self._balance: self._balance -= amount
5.
acc = Account(100) acc._______(50) print(acc.get_balance())

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Account:
B def __init__(self, bal)
C self._balance = bal
D def get_balance(self):
E return self._balance
Question 2
A class Player:
B def __init__(self):
C self._score = 0
D def add_points(n):
E self._score += n
Question 3
A Class Student:
B def __init__(self, name):
C self._name = name
D def get_name(self):
E return self._name
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
Question 5
A class Pet:
B def _init_(self, name):
C self._name = name
D def get_name(self):
E return self._name

Answer Key

Encapsulation in OOP
Section A: Quick Questions
Q1
A — Bundling data and the methods that use it into one class
Q2
B — A method deposit() changes self._balance after checking the amount
Q3
A — To stop it being set to invalid values like a negative age
Q4
B — It is a convention telling programmers to use methods instead of changing it directly
Q5
A — class Pet: with self.name and a speak() method inside it
Section B: Match the Pairs
Q1
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
Q2
def deposit(self, amount): → A method in the same class that works on the object's data, keeping data and behaviour bundled together
Q3
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
Q4
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()
Q5
account._balance = -500 → Changes the protected attribute directly from outside the class, skipping the method that checks the value, so it breaks encapsulation
Q6
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
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
class Account: def __init__(self, balance): self._balance = balance
Q2
def balance(self): return self._balance
Q3
def deposit(self, amount): if amount > 0: self._balance += amount
Q4
def withdraw(self, amount): if amount <= self._balance: self._balance -= amount
Q5
acc = Account(100) acc.deposit(50) print(acc.get_balance())
Section E: Spot the Error
Q1
B — The def line is missing its colon. It should be 'def __init__(self, bal):'. The constructor sets up the object's protected data.
Q2
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):'.
Q3
A — The keyword class must be all lowercase in Python. It should be 'class Student:'. This line bundles the data and methods together.
Q4
C — Without 'self.' this makes a local variable, not an attribute stored in the object. It should be 'self._cash = 0'.
Q5
B — The constructor needs two underscores on each side: __init__. With one underscore Python will not run it when an object is created.