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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

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.__weapon

Polymorphism

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 axe

The 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 engine

UML class diagrams

Each class is a box with three compartments: name; attributes (visibility, name, type); methods (visibility, name, parameters, return type).

CharacterWarrior
# 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.

  1. `class Warrior(Character)`: inheritance.
  2. `super().__init__(...)`: calling the parent constructor.
  3. `_name`, `_strength`: protected attributes; `__weapon`: private attribute.
  4. 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.

Key idea: Inheritance = 'is a' (hollow triangle to parent). Composition = 'has a', owned parts (filled diamond at the whole). Polymorphism = same call, different behaviour, usually by overriding.

Check yourself

  1. 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.

  2. 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).

  3. 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.

  4. 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).

  5. 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.

  6. 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.

  7. 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.

  8. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.