24.1 Introduction to Inheritance
A teacher and a student are both people: they share a name and an age, and both can introduce themselves. Writing that shared code twice is wasteful. Inheritance lets you write the shared parts once in a parent class, then create child classes that automatically get them and add their own extras.
By the end of this page you will define a parent class, build a child class that inherits from it, reuse the parent's constructor with super(), override a method to change its behaviour, and use polymorphism so a single loop can call the same method on a mixed list of objects.
- Inheritance, SUPER, method overriding and polymorphism.
- It builds on classes from Part 1 and encapsulation from Part 2.
| Term | Meaning |
|---|---|
| Inheritance | A child (sub) class automatically gets the attributes and methods of a parent (super) class. The relationship is "is-a": a Car is a Vehicle. |
| Superclass (parent) | The class that provides the shared attributes and methods. |
| Subclass (child) | The class that inherits from the parent and may add or change features. |
| super() | In Python, a way to call the parent class’s method — most often its constructor. In pseudocode the keyword is SUPER. |
| Method overriding | A child class redefining a method it inherited, so its own version runs instead. |
| Polymorphism | Calling the same method name on different objects and getting each class’s own behaviour. |
24.2 Inheritance — Reusing a Parent Class
- Mark scheme: Without inheritance you copy the same attributes and methods into every related class, and a change means editing them all.
- Inheritance writes the shared code once in the parent and hands it to every child.
- Cambridge lists inheritance as core OOP terminology and examines child classes directly in Paper 4.
- Examiner focus: Inheritance is part of the OOP characteristics tested on Paper 3 (e.g. matching paradigms to “class, inheritance, encapsulation and polymorphism”) and is written in Paper 4.
- The Pseudocode Guide gives the keywords
INHERITSandSUPER.
In Python a child class names its parent in brackets: class Car(Vehicle). The child then has every attribute and method of Vehicle for free, and can add its own.
class Vehicle: # parent (superclass)
def __init__(self, brand, model):
self.brand = brand
self.model = model
def start(self):
print(self.brand, self.model, "is starting.")
class Car(Vehicle): # child inherits Vehicle
def __init__(self, brand, model, doors):
super().__init__(brand, model) # reuse parent constructor
self.doors = doors
def honk(self):
print(self.brand, "goes beep!")
car = Car("Toyota", "Corolla", 4)
car.start() # inherited from Vehicle
car.honk() # defined in CarThe pseudocode equivalent uses the keyword INHERITS:
CLASS Vehicle
PRIVATE Brand : STRING
PUBLIC PROCEDURE NEW(GivenBrand : STRING)
Brand <- GivenBrand
ENDPROCEDURE
ENDCLASS
CLASS Car INHERITS Vehicle
PRIVATE Model : STRING
PUBLIC PROCEDURE NEW(GivenBrand : STRING, GivenModel : STRING)
SUPER.NEW(GivenBrand)
Model <- GivenModel
ENDPROCEDURE
ENDCLASSPerson → Teacher and Student (is-a)
Person
name, age
introduce()
Teacher
+ subject
+ teach()
Student
+ examMark
+ study()
Teacher and Student inherit name, age and introduce() from Person, then add their own features.
- A child class is written
class Child(Parent)in Python orCLASS Child INHERITS Parentin pseudocode. - It gets all the parent's attributes and methods, and can add more.
- Use it only for an “is-a” relationship.
- Inheritance is for “is-a”.
- If the relationship is “has-a” (a Car has an Engine), that is containment, not inheritance — see the Containment page.
- Mixing them up is a classic design error examiners look for.
24.2 Inheritance — Reusing a Parent Class
24.3 SUPER and Method Overriding
- A child usually needs the parent to set up its shared attributes — repeating that code defeats the point of inheritance.
super()calls the parent's constructor so the child reuses it.- Overriding then lets the child replace an inherited method with its own version when the behaviour should differ.
Inside the child constructor, super().__init__(...) runs the parent constructor first, then the child adds its extra attribute. To override, the child simply defines a method with the same name — its version runs instead of the parent's.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print("I am", self.name)
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age) # reuse parent constructor
self.subject = subject
def introduce(self): # override
print("I am", self.name, "and I teach", self.subject)
t = Teacher("Mrs Rahman", 42, "Computer Science")
t.introduce()
# Output: I am Mrs Rahman and I teach Computer Science- Step 1 — inherit: class Student(Person):.
- Step 2 — call parent: super().__init__(name, age).
- Step 3 — add extra: store exam_mark.
- Mirror the Student example.
- Call the parent constructor with
super().__init__(...)(Python) orSUPER.NEW(...)(pseudocode) before adding the child's own attributes. - To override, define a method with the same name in the child.
- A child constructor that forgets
super().__init__()loses the parent's attributes — the object will be missingname/ageand later code will crash. - The examiner specifically checks for the SUPER call.
24.3 SUPER and Method Overriding
24.4 Polymorphism
- Once several classes share a method name, you can treat their objects the same way and let each one behave differently — that is polymorphism.
- It means one loop can call
introduce()on a mixed list of teachers and students, and each prints its own version. - This is where overriding pays off.
Put different objects in one list and loop over them, calling the shared method. Python runs each object's own overridden version automatically.
people = [Teacher("Mrs Rahman", 42, "CS"),
Student("Rumaisa", 17, 90),
Student("Aymaan", 17, 78)]
for person in people:
person.introduce() # each runs its own version- Each object runs its own speak().
- Polymorphism picks each object's own version.
- Polymorphism = the same method call (
person.introduce()) produces different behaviour depending on the object's class. - It works because each subclass overrides the shared method.
24.4 Polymorphism
24.5 Full Exam-Style Question
- A school stores people in
PeopleData.txt. - Each line is either a student
S,Rumaisa,17,90or a teacherT,Mrs Rahman,42,Computer Science.
(a) Write a base class Person with attributes Name and Age, a constructor, and a procedure DisplayDetails() that outputs them. [4]
(b) Write a class Student that inherits Person, adds ExamMark, uses the parent constructor, and overrides DisplayDetails() to also output the mark. [4]
(c) Write a procedure ReadData() that reads each line of PeopleData.txt to the end of file, splits on commas, builds a Student for S lines and a Teacher for T lines, stores each in PeopleArray, increments TotalPeople, and uses exception handling. [8]
- (a) Person: __init__(self, name, age) storing Name and Age; DisplayDetails prints both.
- (b) Student(Person): __init__ with extra ExamMark, super().__init__(name, age), override DisplayDetails to also print Mark.
- (c) try: open; while line != "": parts = split(","); if parts[0]=="S": Student else Teacher; store in PeopleArray; TotalPeople += 1; readline; close; except: print error.
24.5 Full Exam-Style Question
✓ Key Points Summary
24.6 Practice Tasks
Fifteen exam-style tasks. Click Hint for bullet-point guidance, then Help to reveal a worked Python solution.
Question Bank
Answer all questions, then press Submit Quiz to see your score.
Question 1Multiple Choice
Inheritance is a relationship like:
Question 2Multiple Choice
In Python, a child class is written:
Question 3True / False
In pseudocode, a child class is written CLASS Child INHERITS Parent.
Question 4Multiple Choice
What does super().__init__(...) do?
Question 5Multiple Choice
Method overriding is:
Question 6Multiple Choice
Polymorphism means:
Question 7True / False
A child constructor that forgets super().__init__() loses the parent's attributes.
Question 8Multiple Choice
Which relationship is inheritance (is-a)?
Question 9Multiple Choice
How does polymorphism work in a loop?
Question 10Multiple Choice
In pseudocode, the parent constructor is called with:
Question 11Multiple Choice
To override a method, the child:
Question 12Multiple Choice
Given animals = [Dog(), Cat(), Dog()] and a loop calling a.speak(), what is the output?
Answer all 12 questions to enable submission.