What is Object-Oriented Programming?
OOP is a way of organising code around "objects" โ bundles of data and the functions that operate on them. In HKDSE ICT Paper 1B, OOP appears in 65% of papers.
Real-world analogy: a class is a blueprint (like a "Student" concept), and objects are actual instances (like Chan Siu Ming, Lee Mei Ling).
Defining Your First Class
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
# Create objects (instances)
s1 = Student("Chan", 17)
s2 = Student("Lee", 16)
print(s1.name) # Chan
print(s2.age) # 16
What's happening?
class Student:โ defines a new class__init__โ the constructor, called when an object is createdselfโ refers to the object itself (required first parameter)self.name = nameโ stores data on the object
Adding Methods
class Student:
def __init__(self, name, score):
self.name = name
self.score = score
def get_grade(self):
if self.score >= 80: return 'A'
elif self.score >= 65: return 'B'
elif self.score >= 50: return 'C'
else: return 'F'
def introduce(self):
print(f"I'm {self.name}, grade {self.get_grade()}.")
s = Student("Chan", 85)
s.introduce() # I'm Chan, grade A.
Multiple Objects
students = [
Student("Chan", 85),
Student("Lee", 72),
Student("Wong", 45),
]
for s in students:
s.introduce()
Updating Object State
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds!")
return False
self.balance -= amount
return True
def show(self):
print(f"{self.owner}: ${self.balance}")
acc = BankAccount("Chan", 1000)
acc.deposit(500)
acc.withdraw(200)
acc.show() # Chan: $1300
Inheritance: Building on Existing Classes
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hi, I'm {self.name}.")
class Student(Person): # Student inherits from Person
def __init__(self, name, age, school):
super().__init__(name, age) # Call parent __init__
self.school = school
def study(self):
print(f"{self.name} is studying at {self.school}.")
s = Student("Chan", 17, "Victoria SS")
s.greet() # Inherited from Person
s.study() # Defined in Student
Class Attributes vs Instance Attributes
class Student:
school = "Hong Kong Secondary School" # class attribute (shared)
def __init__(self, name):
self.name = name # instance attribute (per object)
s1 = Student("Chan")
s2 = Student("Lee")
print(s1.school) # Both share the same school
print(s2.school)
print(s1.name, s2.name) # Different names
HKDSE-Style Example
A typical Paper 1B question:
class Book:
def __init__(self, title, author, copies):
self.title = title
self.author = author
self.copies = copies
self.borrowed = 0
def borrow(self):
if self.borrowed < self.copies:
self.borrowed += 1
return True
return False
def return_book(self):
if self.borrowed > 0:
self.borrowed -= 1
def available(self):
return self.copies - self.borrowed
# Library simulation
book = Book("Python Guide", "L. Yau", 3)
book.borrow()
book.borrow()
print(book.available()) # 1
book.return_book()
print(book.available()) # 2
OOP Checklist for HKDSE
- Always include
__init__withselfas first parameter - Every method must have
selfas first parameter - Access attributes via
self.attributeinside methods - Create objects with
ClassName(args) - Call methods with
obj.method()
Practise OOP with Real Exercises
Build a Student class, a BankAccount class, a Library system โ all in PyForm's browser IDE.
Try OOP Free โ