Static polymorphism determines which method to call at compile time, usually through method overloading. Dynamic polymorphism determines the method at runtime, using method overriding and the actual type of an object.
How Each Type Works
With method overloading, a class contains methods with the same name but different parameter lists. The compiler chooses the matching method by examining the number, order, and declared types of the arguments.
class Printer {
void print(int value) { }
void print(String value) { }
}
Printer p = new Printer();
p.print(5); // Selects print(int)
p.print("Hello"); // Selects print(String)
With method overriding, a subclass provides its own implementation of an inherited method. A superclass reference can refer to a subclass object, and dynamic binding selects the implementation belonging to the object's actual runtime type.
class Animal {
void speak() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void speak() { System.out.println("Bark"); }
}
Animal pet = new Dog();
pet.speak(); // Outputs "Bark"
Although pet is declared as Animal, the object created is a Dog. Therefore, the overridden Dog.speak() method executes.
| Feature | Static polymorphism | Dynamic polymorphism |
|---|---|---|
| Decision time | Compile time | Runtime |
| Main mechanism | Method overloading | Method overriding |
| Method selection depends on | Argument list and declared types | Actual object type |
| Inheritance required | No | Usually yes |
| Alternative name | Compile-time polymorphism | Runtime polymorphism |
A common misconception is that any repeated method name demonstrates dynamic polymorphism. Overloaded methods are selected at compile time; only overridden methods called through an appropriate superclass reference demonstrate runtime method selection.
IB Exam Technique
For B3.2 Fundamentals of OOP for multiple classes (HL only), identify whether the example uses overloading or overriding, state when method selection occurs, and trace the method that executes. When explaining dynamic polymorphism, distinguish clearly between the reference's declared type and the object's actual type.