Community resourceWorksheet
OCR H446 1.2.4 Object Oriented Programming 3
Part 4 of 6 · H446 Data Structures and OOP
This worksheet introduces inheritance and polymorphism in object-oriented programming. Pupils explore how subclasses share common behaviour from a superclass and override specific methods. Tasks include implementing subclasses, overriding methods, and understanding design advantages and pitfalls. The resource is moderately challenging and suitable for learners familiar with basic classes and methods.
Shared by Chris H.Verified teacher
- 12 cells
- About 50 minutes
- CC BY-SA 4.0
- Shared 29 Jul 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Inheritance and polymorphism
OCR H446 references: 1.2.4(e), 2.2.1(f) and Appendix 5d
Large programs often contain several kinds of object that share some behaviour but differ in other ways. A streaming service might send email, text and app notifications. A game might contain several character types that all move but attack differently.
Inheritance can place genuinely shared features in one superclass. Polymorphism then allows the program to use a common method call while each subclass provides the behaviour appropriate to its own objects.
By the end, you should be able to:
- identify superclass and subclass relationships
- read
inherits,super.newand overriding in OCR pseudocode - implement inheritance and method overriding in Python
- explain when inheritance is and is not an appropriate design choice
Reuse common behaviour, specialise what changes
Suppose a transport app stores cars, buses and bicycles. Every vehicle may need a name, but only a bus has a route number. Repeating the same name-handling code in every class would create duplication.
A superclass, also called a parent class, contains the attributes and methods shared by the more specialised classes.
A subclass, also called a child class, inherits those members. It can then:
- use inherited behaviour unchanged
- add new attributes or methods
- provide a different version of an inherited method
Overriding
Overriding means that a subclass defines its own version of a method with the same name. The method still represents the same general action, but the details change for that subclass.
Polymorphism
Polymorphism allows the same method call to work with objects from different classes. For example, a loop can call notification.send() without containing separate if statements for email and text messages. The object's actual class determines which send() method runs.
OCR pseudocode
class Vehicle
protected name
public procedure new(givenName)
name = givenName
endprocedure
public function description()
return name
endfunction
endclass
class Bus inherits Vehicle
private route
public procedure new(givenName, givenRoute)
super.new(givenName)
route = givenRoute
endprocedure
public function description()
return name + " on route " + route
endfunction
endclass
super.new(givenName) runs the superclass constructor so that the inherited part of the object is initialised correctly.
Python equivalent
class Vehicle:
def __init__(self, name):
self.name = name
def description(self):
return self.name
class Bus(Vehicle):
def __init__(self, name, route):
super().__init__(name)
self.route = route
def description(self):
return self.name + " on route " + self.route
Use inheritance only for a genuine is-a relationship: a bus is a vehicle. Two classes merely sharing one attribute is not enough. An unsuitable hierarchy can force subclasses to inherit behaviour that does not make sense for them.
class Notification:
def __init__(self, recipient):
self.recipient = recipient
def send(self):
return "Notification for " + self.recipient
class EmailNotification(Notification):
def send(self):
return "Email sent to " + self.recipient
class TextNotification(Notification):
def send(self):
return "Text sent to " + self.recipient
messages = [EmailNotification("Amina"), TextNotification("Noah")]
for message in messages:
print(message.send())Class that supplies common behaviour: ______. Class that inherits and specialises behaviour: ______. Defining a replacement version of an inherited method: ______. One method call producing class-appropriate behaviour: ______. Mechanism used to form the parent-child relationship: ______.- superclass
- subclass
- overriding
- polymorphism
- inheritance
The loop calls message.send() for both an EmailNotification and a TextNotification. Which OOP idea is most directly demonstrated?
- AEncapsulation, because all attributes are private
- BComposition, because one object is stored inside another
- CInstantiation, because the loop creates a new class
- DPolymorphism, because the same method call uses different implementations
Checkpoint: separate what is inherited from what is overridden
In the notification example:
- both subclasses inherit the
recipientattribute - each subclass overrides
send() - the loop can call
send()without knowing the exact subclass in advance
That final point is the polymorphism. The loop is simpler because the decision about how to send is placed inside each class.
Guided modification
Run the animal example, then add a Cat subclass whose speak() method returns Milo says meow. Compare the Dog and Cat definitions: both inherit the same name handling but specialise the sound.
Independent rectangle task
Create the subclass in this order:
- Make
Rectangleinherit fromShape. - Call the superclass constructor with
super().__init__(name). - Store
widthandheight. - Override
area()to return their product. - Create the required rectangle.
Use the exact class, method and object names so that the checks can inspect the result.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return self.name + " makes a sound"
class Dog(Animal):
def speak(self):
return self.name + " says woof"
dog = Dog("Rex")
print(dog.speak())
# Add Cat as another subclass of Animal.
# Create cat = Cat("Milo") and print its speak() result.class Shape:
def __init__(self, name):
self.name = name
def area(self):
return 0
# Define Rectangle as a subclass of Shape.
# Its constructor receives name, width and height.
# It must call the superclass constructor, store width and height,
# and override area() to return width * height.
rectangle = None # Replace with a Rectangle called "screen", width 8, height 5.A school system has Person, Student and Teacher classes. Explain one advantage of making Student and Teacher subclasses of Person, and one possible design problem if the shared superclass is given too many unrelated responsibilities.
Link the advantage to common data or methods. Link the problem to coupling, irrelevant behaviour or difficulty changing the design.
Students type their answer here.
Independent task: override a price
All tickets have an event name and a base price. A student ticket is a ticket, but its price() method returns half the base price.
Complete the subclass and supplied objects. The list comprehension calls the same price() method on a Ticket and a StudentTicket. The expected prices list is [20, 10.0].
Keep the solution simple: use inheritance, super() and one overridden method.
class Ticket:
def __init__(self, event, base_price):
self.event = event
self.base_price = base_price
def price(self):
return self.base_price
class StudentTicket(Ticket):
# Add a constructor that uses the superclass constructor.
# Override price() so it returns half the base price.
pass
standard = Ticket("Robotics Final", 20)
student = None # Create the equivalent StudentTicket.
prices = [] # Replace with prices from both objects, using a loop or comprehension.State what method overriding means. Then explain how overriding enables polymorphism.
Use the words superclass or subclass and method call in your answer.
Students type their answer here.