Part 1: Syllabus Review
This section provides a comprehensive review of **The Object-Oriented Paradigm**. Click on each topic to expand it. Opening a new topic will close the previous one.
Interactive Exploration: The Pillars of OOP
Engage with the core principles of OOP. These interactive examples demonstrate how Encapsulation, Inheritance, and Polymorphism work in practice.
Encapsulation
Encapsulation bundles data (attributes) and methods into a single class. A key part is **information hiding**, where attributes are made 'private' (e.g. `_hp`) to prevent direct, uncontrolled access. Public methods (`take_damage`, `heal`) are provided to ensure data is modified in a valid way.
class Character:
def __init__(self, max_hp):
self._max_hp = max_hp
self._hp = max_hp # Private
def take_damage(self, amount):
self._hp -= amount
if self._hp < 0:
self._hp = 0
def heal(self, amount):
self._hp += amount
if self._hp > self._max_hp:
self._hp = self._max_hp
Try the interactive example:
Character HP: 100 / 100
Inheritance
Inheritance allows a **subclass** (`Car`, `Truck`) to acquire the properties and methods of a **superclass** (`Vehicle`). This promotes code reusability. Subclasses can also have their own unique attributes.
# Superclass
class Vehicle:
def __init__(self, colour):
self.speed = 0
self.colour = colour
# Subclass
class Car(Vehicle):
def __init__(self, colour, doors):
super().__init__(colour)
self.num_doors = doors
# Subclass
class Truck(Vehicle):
def __init__(self, colour, capacity):
super().__init__(colour)
self.cargo_capacity = capacity
Hover over the diagram:
Polymorphism
Polymorphism ("many forms") allows objects of different classes to respond to the same method call (e.g., `speak()`) in different ways. This makes code more flexible.
class Animal:
def speak(self):
pass # Abstract method
class Dog(Animal):
def speak(self):
return "Woof!" # Override
class Cat(Animal):
def speak(self):
return "Meow!" # Override
# Both objects respond to .speak()
my_dog = Dog()
my_cat = Cat()
Click the buttons to see the output:
Press a button...
HSC Sample Assessment Task
Object-Oriented Paradigm
General Instructions
- Reading time – 2 minutes
- Working time – 30 minutes
- This is a digital simulation of a NESA-style exam.
Section I – 5 marks
Attempt Questions 1-5. Allow about 5 minutes for this section.
Section II – 15 marks
Attempt Question 6. Allow about 25 minutes for this section.
Stimulus
A developer is creating a simple text-based adventure game. In the game, there are different types of characters that interact with the world. All characters have a name and health points (HP). The two primary character types are:
- Players, who are controlled by the user. Players also have an inventory to store items they collect.
- Enemies, which are non-player characters. Enemies have a specific `attack_power` attribute that determines how much damage they deal.