📄 Paper 2 · 4.6 Hardware & Software
4.6.3b Object-Oriented Programming — In Practice
AQA 7517 · A-Level Computer Science · ~14 min read

Writing Classes in Python

Python uses the class keyword to define a class. The constructor is __init__(self, ...). self refers to the current instance.

class Animal:
    def __init__(self, name, sound):
        self.__name = name     # private attribute
        self.__sound = sound

    def get_name(self):        # getter
        return self.__name

    def speak(self):
        return self.__name + " says " + self.__sound

class Dog(Animal):             # Dog inherits Animal
    def __init__(self, name):
        super().__init__(name, "Woof")  # call parent constructor

    def fetch(self):
        return self.__name + " fetches the ball!"

Using super()

super() calls a method from the parent class. Most commonly used in the subclass constructor to call the parent's __init__ and avoid duplicating initialisation code.

Access Modifiers in Python

Python uses naming conventions rather than strict keywords:

  • Public: self.name — accessible everywhere
  • Protected: self._name — convention; accessible within class and subclasses
  • Private: self.__name — name mangled; not directly accessible outside the class

Getters and Setters

Public methods to read (get) and modify (set) private attributes. Setters can include validation logic:

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance

    def get_balance(self):
        return self.__balance

    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount
        else:
            raise ValueError("Amount must be positive")

Class Diagrams (UML)

UML class diagrams show: class name (top), attributes (middle), methods (bottom). Arrows show inheritance (hollow triangle) and association.

  • + (plus) = public
  • - (minus) = private
  • # (hash) = protected

Association, Aggregation, Composition

  • Association: one class uses another (e.g. Student uses Library)
  • Aggregation: "has a" relationship — parts can exist independently (e.g. Department has Teachers; teachers exist without the department)
  • Composition: strong "has a" — parts cannot exist independently (e.g. House has Rooms; rooms don't exist without the house)

Overloading vs Overriding

  • Method overriding: subclass replaces a parent's method with its own version
  • Method overloading: same method name with different parameter types/counts (Python handles this via default parameters)
Exam tip: AQA may give you a class definition and ask you to: (a) trace through instantiation and method calls; (b) extend the class with a subclass; (c) add a getter/setter. Know how to read UML class diagrams. Understand super() and why it's used. Know the difference between overriding and overloading. Be able to write Python OOP code clearly and correctly.
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.6.3b OOP in Practice

8 questions · instantly marked · AQA 7517 standard

Q1In Python OOP, what is the purpose of self in a method definition?[2]
✅ Mark scheme
Mark scheme
self refers to the current instance of the class [1]; it allows the method to access and modify the specific object's attributes [1].
Q2Explain the purpose of super() in a subclass constructor. Why is it better than re-writing the parent's initialisation code?[3]
✅ Mark scheme
Mark scheme
super() calls the parent class's method from within the subclass [1]; calling super().__init__() in the subclass constructor ensures the parent's initialisation code runs without needing to duplicate it [1]; this reduces code duplication and means changes to the parent constructor only need to be made in one place [1].
Q3In Python, what is the difference between self.name, self._name, and self.__name?[3]
✅ Mark scheme
Mark scheme
self.name: public — accessible anywhere [1]; self._name: protected by convention — should only be used within the class and subclasses, but not enforced [1]; self.__name: private — name-mangled by Python, cannot be directly accessed outside the class [1].
Q4Write a Python class called Shape with a private attribute __colour, a constructor that sets it, and a getter method get_colour().[4]
✅ Mark scheme
Mark scheme
class Shape: [1]; __init__(self, colour): with self.__colour = colour [1]; def get_colour(self): [1]; return self.__colour [1].
Q5Explain the difference between method overriding and method overloading. Which does Python support natively?[3]
✅ Mark scheme
Mark scheme
Method overriding: a subclass provides its own version of a method inherited from the parent class [1]; method overloading: the same method name is used with different parameter types or numbers — allowing different behaviour based on arguments [1]; Python natively supports overriding; overloading is simulated using default parameters or *args since Python doesn't support true overloading [1].
Q6In a UML class diagram, what do the symbols +, -, and # represent?[3]
✅ Mark scheme
Mark scheme
+ (plus) = public — accessible everywhere [1]; - (minus) = private — only accessible within the class [1]; # (hash) = protected — accessible within the class and its subclasses [1].
Q7Distinguish between aggregation and composition with an example of each.[4]
✅ Mark scheme
Mark scheme
Aggregation: "has a" relationship where parts can exist independently of the whole [1]; example: Department has Teachers — teachers can exist even if the department is dissolved [1]. Composition: strong "has a" — parts cannot exist independently [1]; example: House has Rooms — rooms cannot exist without the house [1].
Q8Why should a setter method for a bank account balance include validation, rather than allowing direct attribute modification?[2]
✅ Mark scheme
Mark scheme
A setter can validate the input before modifying the balance (e.g. reject negative deposits) [1]; direct attribute access would bypass this validation, allowing the balance to be set to invalid values and breaking encapsulation [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 8
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — OOP in Practice

10 questions · 10 minutes

← 4.6.3a OOP Concepts
48 of 70 · AQA 7517
4.7.1 Internal Hardware →