IB Computer Science HL · Year 2 · Lesson 17

Polymorphism

Use one interface with different subclass behaviour, and distinguish Python runtime overriding from the broader concept of static polymorphism.

Paper 250 minutesB3.2.2 · HL only
Targets

Same message, different behaviour

2 / 10
  • explain how polymorphism improves flexibility and reuse;
  • construct Python code that uses method overriding;
  • trace dynamic polymorphic behaviour at runtime;
  • understand static polymorphism conceptually and avoid false signature-overloading patterns in Python.
B3.2.2
Learn + retrieve

Rapid Recall Deck

3 / 10

Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.

Dynamic polymorphism

Method overriding changes behaviour at runtime

4 / 10
class Notification:
    def send(self):
        return "generic message"

class EmailNotification(Notification):
    def send(self):
        return "email sent"

class SMSNotification(Notification):
    def send(self):
        return "SMS sent"

items = [EmailNotification(), SMSNotification()]

for item in items:
    print(item.send())

Predict the runtime behaviour

Common interface: every object is called with send().
Runtime type: decide which overridden implementation belongs to each object.

The output is email sent followed by SMS sent. The same method call is used, but Python dispatches to the implementation defined by each object’s actual class.

B3.2.2 · Construct

Build a common interface with different implementations

5 / 10
RequirementsA parent class Report has a method format(). TextReport should return "TEXT" and PDFReport should return "PDF".

Construct the overriding classes

class Report:
    def format(self):
        return "UNKNOWN"

class TextReport(Report):
    def format(self):
        return "TEXT"

class PDFReport(Report):
    def format(self):
        return "PDF"
Static vs dynamic

Know the concept without writing false Python

6 / 10

Explain the flexibility

A function receives a list of report objects and calls format() on each one without checking object types. Explain why this is easier to extend than a long chain of if type == ... conditions.

New subclasses can provide the same interface by overriding format(). Existing calling code can remain unchanged, reducing type-specific branching and improving extensibility/reuse.

Do not invent Python overloads

class Example:
    def area(self, width):
        return width * width

    def area(self, width, height):
        return width * height

Why does this not create two overloaded methods in Python?

The second definition replaces the first. Python does not use Java-style compile-time signature overloading for methods. A single method with a clear optional-parameter design would be needed instead.

Concept precision

Two forms, different dispatch timing

7 / 10

Static polymorphism — conceptual boundary

In languages that support compile-time overloading, the same operation name can be resolved from parameter types/signatures before runtime. That is a common form of static polymorphism. Because the implementation can be selected before runtime, this can improve code efficiency by avoiding the same kind of runtime object dispatch used by dynamic polymorphism for that call.

Python course ruleFor Python construction, demonstrate polymorphism with valid Python behaviour such as method overriding. Understand static polymorphism as a general OOP concept; do not define the same Python method name twice and expect signature-based overload resolution.

Explain one difference between static and dynamic polymorphism.

Static polymorphism selects behaviour before runtime in languages with compile-time overload resolution. Dynamic polymorphism selects behaviour at runtime based on the actual object, for example through overridden methods.

Exam lens

Teacher-created Paper 2 practice

8 / 10

Paper 2 mini-set

1. Construct a parent class Payment with process() and two child classes CardPayment and CashPayment that override it with different return strings.

2. Explain how a loop that calls process() on mixed payment objects demonstrates dynamic polymorphism.

3. Distinguish dynamic polymorphism from the concept of static polymorphism.

The code should define the same method interface in the parent and both child classes; the loop uses one call while runtime dispatch chooses the child implementation. Dynamic dispatch occurs at runtime; static polymorphism is resolved before runtime in languages that support compile-time overloading.

Exit check

Can you recognize the common interface?

9 / 10

Exit check

Why is “different method names for every subclass” usually not polymorphism?

Polymorphism relies on a common interface. If calling code must know a different method name for each type, it cannot treat the objects uniformly through one shared operation.

Homework

Finish the learning cycle

10 / 10

IA — main task

Continue Criterion D implementation/testing and capture one test that exercises different object states or behaviours.

Paper 2 — short practice

Construct the Payment hierarchy and trace a list containing one object of each child class.