All subjects · Computer Science · WCP03 Advanced Principles of Computer Science
OOP: inheritance, composition, polymorphism and UML class diagrams
Edexcel IAL Computer Science · WCP03 Advanced Principles of Computer Science
The power of OOP comes from relationships between classes. Inheritance and composition reuse code; polymorphism lets one call behave differently for different objects. UML class diagrams show all of this at a glance.
Inheritance
A subclass (child) inherits the attributes and methods of a superclass (parent) and can add its own or change inherited ones. It models an 'is a' relationship: a Warrior *is a* Character.
class Character:
def __init__(self, name, strength):
self._name = name
self._strength = strength
def display(self):
return self._name + " " + str(self._strength)
class Warrior(Character):
def __init__(self, name, strength, weapon):
super().__init__(name, strength)
self.__weapon = weapon
def display(self): # overrides the parent method
return "Warrior " + self._name + " with " + self.__weaponPolymorphism
Polymorphism ('many forms') means objects of different classes respond to the same method call in their own way. Most often this is done by overriding: a subclass redefines an inherited method.
for c in [Character("Ana", 5), Warrior("Bo", 9, "axe")]:
print(c.display())
# Ana 5
# Warrior Bo with axeThe loop does not need to know which class each object is: new subclasses can be added without changing it.
Composition
Composition models a 'has a' relationship: an object is built from other objects that it owns. A Car *has an* Engine; if the car is destroyed, so is its engine. Composition is often more flexible than inheritance, because parts can be swapped.
class Engine:
def start(self): return "vroom"
class Car:
def __init__(self):
self.__engine = Engine() # the car owns its engineUML class diagrams
Each class is a box with three compartments: name; attributes (visibility, name, type); methods (visibility, name, parameters, return type).
- Inheritance: a line with a hollow triangle pointing at the parent.
- Composition: a line with a filled diamond at the owning (whole) class.
- Visibility: + public, - private, # protected.
| Character | Warrior |
|---|---|
| # name: String | - weapon: String |
| # strength: Integer | |
| + display(): String | + display(): String |
| + getStrength(): Integer |
Warrior inherits name, strength and getStrength(); it overrides display(). Arrow: Warrior to Character with a hollow triangle at Character.
Worked example
Identify the OOP features in the Character and Warrior code.
- `class Warrior(Character)`: inheritance.
- `super().__init__(...)`: calling the parent constructor.
- `_name`, `_strength`: protected attributes; `__weapon`: private attribute.
- Warrior's `display()` replaces Character's: overriding, giving polymorphism when display() is called on a list of mixed objects.
Answer: Inheritance, protected/private attributes, overriding and polymorphism.
Check yourself
- What is polymorphism?
- A class having many attributes
- Hiding data in a class
- Creating many objects from one class
- Objects of different classes responding to the same method call in different ways
Show answer
Objects of different classes responding to the same method call in different ways. For example display() behaves differently for Character and Warrior.
- Which relationship is best modelled by inheritance?
- A House has a Kitchen
- A Lorry is a Vehicle
- A Library has Books
- A Car has an Engine
Show answer
A Lorry is a Vehicle. Inheritance models 'is a'; the others are 'has a' (composition or aggregation).
- Which relationship is composition?
- A Cat is an Animal
- A Square is a Shape
- A Teacher is a Person
- A Car has an Engine that it creates and owns
Show answer
A Car has an Engine that it creates and owns. Composition is 'has a', with the part owned by the whole.
- In a UML class diagram, what does a line with a hollow triangle mean?
- Inheritance, with the triangle pointing at the parent class
- Composition, with the triangle at the part
- A private method
- A method call
Show answer
Inheritance, with the triangle pointing at the parent class. Hollow triangle = generalisation (inheritance).
- In a UML class diagram, what does a filled diamond show?
- Inheritance
- A public attribute
- Composition, with the diamond at the class that owns the part
- An abstract class
Show answer
Composition, with the diamond at the class that owns the part. Filled diamond = composition.
- What is method overriding?
- A subclass providing its own version of a method it inherited
- Calling a method twice
- Deleting a method from the parent class
- Making a method private
Show answer
A subclass providing its own version of a method it inherited. Overriding is the usual way to achieve polymorphism.
- In the code, which members of Character can Warrior's methods use directly?
- Only __weapon
- None, because they are protected
- _name, _strength and display()
- Only display()
Show answer
_name, _strength and display(). Protected members are available to subclasses.
- Which is a benefit of inheritance?
- It makes every attribute private
- It removes the need for constructors
- It makes programs run in parallel
- Common code is written once in the parent and reused by every subclass
Show answer
Common code is written once in the parent and reused by every subclass. Reuse reduces duplication and errors.
Sources
- Pearson Edexcel International Advanced Subsidiary and Advanced Level in Computer Science specification (2026), Issue 1
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 paper, resource booklet and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.