← VCE Software Development U4O1

Protected attributes and methods

A single underscore prefix marks a protected member: by convention it is for use inside the class and its subclasses only. Python does not enforce it, so outside code can still access it.

Understanding Protected attributes and methods in VCE Software Development U4O1

What it does: The Savings subclass reads the parent's _balance attribute inside its own method, which is the intended use of a protected member. Python does not block this access and no error occurs, but it breaks the convention because the code is outside the class and its subclasses. The underscore tells other programmers that _name is for the class and its subclasses, not for general use. One leading underscore means protected. Two leading underscores make the member private and trigger name mangling.

Common mistakes: Dog must inherit from Pet to use the protected attribute _name. It should be class Dog(Pet): so subclass code can access self._name. A class header must end with a colon. It should be class Savings(Account): so the subclass can use the protected _balance.

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 3–79) and word search (Modules 3–114).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which attribute name marks a protected member in Python?
self._balanceself.balance_
Matches
self.balance = 0 # meant to be protected
↓
The name has no leading underscore, so it is public. A protected attribute needs a single underscore: self._balance = 0.
Applying
Meanings
Term
_name
Meaning
A single underscore prefix marks a protected member: by convention it is for use inside the class and its subclasses only. Python does not enforce it, so outside code can still access it.
Practice
class Vehicle:
Testing
Gaps
= "Rex"
Check Answer
Spots
3 self._name = name
4class Dog:

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

The 'Deer' badge — Forest Streak, 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 attribute name marks a protected member in Python?
A. self._balance ✓
B. self.balance_
A protected member starts with a single underscore, like _balance. An underscore at the end has no special meaning.
2. Python stops outside code from reading a protected attribute such as obj._name.
A. True
B. False ✓
A single underscore is only a convention. Python does not enforce it, so outside code can still read obj._name.
3. A protected attribute _name is intended to be used...
A. Inside the class and its subclasses ✓
B. Anywhere in the program
The underscore tells other programmers that _name is for the class and its subclasses, not for general use.
4. Which subclass method uses a protected attribute from its parent class correctly?
A. def show(self): print(_name)
B. def show(self): print(self._name) ✓
A subclass accesses an inherited protected attribute through self, like self._name. Without self, Python looks for a variable called _name and fails.
5. Why do programmers use a single underscore on an attribute?
A. To signal it is not part of the public interface ✓
B. To make Python block access to it
The underscore is a signal to other programmers. It works by convention and does not enforce anything.

Match the Pairs

self._balance = 0 → Creates a protected attribute: the single underscore signals it is meant for use only inside the class and its subclasses
def _calculate_fee(self): → Defines a protected method that is intended to be called only by the class itself or its subclasses
A subclass can use a protected attribute inherited from its parent class → class Savings(Account): def show(self): print(self._balance)
Protected is a convention, not enforcement: code outside the class can still access the attribute → acc = Account() print(acc._balance)
self.balance = 0 # meant to be protected → The name has no leading underscore, so it is public. A protected attribute needs a single underscore: self._balance = 0.
print(acc._balance) # in main program → Code outside the class and its subclasses should not use a protected attribute directly. Use a public method or property such as acc.get_balance() instead.

Key Term

_name
A single underscore prefix marks a protected member: by convention it is for use inside the class and its subclasses only. Python does not enforce it, so outside code can still access it.

Fill in the Blanks

1. class Pet: _name = "Rex" age = 3
Hint: Type the attribute name that marks it as protected, using a single underscore prefix.
2. class Dog(Pet): def speak(self): return self._name + " barks"
Hint: Type the protected attribute inherited from the parent class that the subclass is allowed to use.
3. class Account: def _check(self): return self.balance > 0
Hint: Type the method name that makes this a protected method, using a single underscore prefix.
4. acc = Account(50) print(acc._balance) # Python allows this
Hint: Type the protected attribute being read from outside the class. Python only uses a convention, so it does not block this.
5. class Saver(Account): def show(self): print(self._check())
Hint: Type the protected method inherited from the parent class that the subclass calls.

Spot the Error

Question 1
A class Pet:
B def __init__(self, name):
C self._name = name
D class Dog:
E def show(self):
F print(self._name)
Line D — Dog must inherit from Pet to use the protected attribute _name. It should be class Dog(Pet): so subclass code can access self._name.
Question 2
A class Savings(Account)
B def add(self, amt):
C self._balance += amt
Line A — A class header must end with a colon. It should be class Savings(Account): so the subclass can use the protected _balance.
Question 3
A class Car:
B def __init__(self, make):
C self.make = make
D def info(self):
E return self._make
Line C — This creates a public attribute called make, not the protected _make. info() then fails because self._make does not exist. It should be self._make = make.
Question 4
A Class Staff:
B def __init__(self, pay):
C self._pay = pay
Line A — The class keyword must be lowercase in Python. It should be class Staff: before the protected attribute _pay is set.
Question 5
A class Student:
B def _init_(self, name):
C self._name = name
Line B — The constructor needs double underscores on each side: __init__. A single underscore only marks a protected member, so Python would not call this when an object is created.

Student with a class code? Join your class →