B3.2.3 · OOP for multiple classes
Polymorphism & overriding
By the end you can
Apply polymorphism and late dynamic method dispatch, distinguishing method overriding from method overloading.
The word comes from the Greek for “many forms”. In object-oriented programming, polymorphism allows a single method call to behave differently depending on the actual type of object executing the instruction at runtime.
Dynamic method dispatch (Late binding)#
In a typed language like Java, a variable can be declared with a superclass type while referencing a subclass instance on the heap:
// Superclass reference holding different subclass objects Shape s1 = new Circle(5.0); Shape s2 = new Rectangle(4.0, 6.0); s1.calculateArea(); // Dispatches dynamically to Circle's formula (pi * r^2) s2.calculateArea(); // Dispatches dynamically to Rectangle's formula (w * h)
Under dynamic method dispatch, the compiler does not lock in the function address at compile time. Instead, the runtime checks the actual object type on the heap and routes the call to the overridden method of that specific subclass.
Overloading versus Overriding#
Students often confuse these two core concepts on IB examinations:
- : A subclass provides a new version of a method already defined in its parent superclass. The signature (name, parameter types, return type) is identical. Uses dynamic binding at runtime.
- Method Overloading: Methods within the same class share the same name but have different parameter lists(e.g.
add(int a, int b)vsadd(double a, double b)). Uses static binding resolved by the compiler.
Check yourself
Polymorphism and dispatch
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 10 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Explain the concept of polymorphism and dynamic binding (late dispatch) in object-oriented programming.
[4]Explain: Give reasons: say what happens AND why, or how one thing causes another.
2.Distinguish between method overloading and method overriding in terms of class location, method signatures, and binding time.
[4]Distinguish: Make the differences between two things clear.
3.Describe how polymorphism allows an array of superclass references to process diverse subclass objects uniformly.
[2]Describe: A detailed account: say what happens, step by step if it is a process.
In a nutshell
- Polymorphism allows different subclasses to implement the same method call with unique, subclass-specific behaviors.
- Dynamic method dispatch (late binding) determines which method implementation to execute at runtime based on the actual heap object type.
- Method overriding occurs when a subclass re-implements a parent method with the exact same signature.
- Method overloading occurs within the same class where methods share a name but differ in parameter lists.
