OOP Polymorphism: Fundamentals, Dynamic Binding & Dispatch
This post is a concept guide to polymorphism in object-oriented programming — complete with exam questions and key terms.
In a Nutshell
Polymorphism lets a single call work on different concrete implementations, with the appropriate method selected based on context, usually at runtime. The result: flexible, extensible software with less coupling to specific types.
Core Definition
Polymorphism is the ability of objects to respond differently to the same message, depending on their actual type. At its heart is dynamic binding: method calls are resolved at runtime through dispatch tables (vtable) to the correct implementation.
There are several forms:
- Subtype polymorphism via interfaces and inheritance
- Parametric polymorphism via generics
- Ad-hoc polymorphism via overloading
Polymorphic design supports the Open Closed Principle: you extend behavior by adding new types rather than modifying existing ones. The requirement is a stable contract—signatures and semantics must remain compatible (Liskov Substitution Principle).
In dynamically typed languages, similar effects emerge through Duck Typing, where focus shifts from static type relationships to available operations.
Key Points for Exams
- Dynamic binding resolved at runtime via vtable
- Subtype polymorphism through inheritance and interfaces
- Method overloading (Overloading) as ad-hoc polymorphism
- Generics enable parametric polymorphism
- Open Closed Principle through polymorphic design
- Liskov Substitution Principle as prerequisite
- Duck Typing in dynamically typed languages
- Dispatch mechanisms for method resolution
Core Components
- Static Binding: Method call resolved at compile time
- Dynamic Binding: Method call resolved at runtime
- Virtual Method Table (vtable): Dispatch table for polymorphic calls
- Method Overriding: Redefining a method in a subclass
- Method Overloading: Multiple methods with the same name
- Generics: Type-independent programming
- Interface Polymorphism: Different implementations of the same interface
- Duck Typing: “If it walks like a duck and quacks…”
Practical Example
// Example: Different forms of polymorphism
interface PaymentMethod {
void pay(double amount);
}
class CreditCard implements PaymentMethod {
@Override
public void pay(double amount) {
System.out.println("Credit card: " + amount + "€ charged");
}
}
class PayPal implements PaymentMethod {
@Override
public void pay(double amount) {
System.out.println("PayPal: " + amount + "€ transferred");
}
}
// Generics (parametric polymorphism)
class Box<T> {
private T content;
public void set(T content) {
this.content = content;
}
public T get() {
return content;
}
}
// Method Overloading (ad-hoc polymorphism)
class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b + 0.1; // Service fee
}
public String add(String a, String b) {
return a + " " + b;
}
}
// Polymorphism in action
public class Main {
public static void main(String[] args) {
// Subtype polymorphism
PaymentMethod[] methods = {
new CreditCard(),
new PayPal()
};
for (PaymentMethod method : methods) {
method.pay(100.0); // Dynamic binding
}
// Generics
Box<String> textBox = new Box<>();
Box<Integer> numberBox = new Box<>();
// Overloading
Calculator calculator = new Calculator();
System.out.println(calculator.add(1, 2)); // 3
System.out.println(calculator.add(1.5, 2.5)); // 4.1
System.out.println(calculator.add("Hello", "World")); // "Hello World"
}
}
Advantages and Disadvantages
Advantages
- Flexibility: Uniform treatment of different types
- Extensibility: Add new implementations without modifying existing code
- Maintainability: Reduced coupling between components
- Reusability: Generic algorithms and data structures
Disadvantages
- Performance Overhead: Dynamic binding comes with a runtime cost
- Complexity: Harder to understand and debug
- Runtime Errors: Type problems discovered only at runtime
- Memory Usage: vtable and additional objects consume resources
Common Exam Questions
-
What’s the difference between overloading and overriding? Overloading: same method name, different parameters. Overriding: a subclass method replaces the base class method.
-
How does dynamic binding work? At runtime, the vtable determines which method to call based on the object’s actual type.
-
What is Duck Typing? “If it walks like a duck and quacks like a duck, then it is a duck”—focus on available methods rather than static types.
-
Why does polymorphism support the Open Closed Principle? New functionality can be added through new types without changing existing code.
Key Sources
- https://en.wikipedia.org/wiki/Polymorphism_(computer_science)
- https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
- https://refactoring.guru/design-patterns/strategy
Continue Your OOP Learning Path
The next article in the OOP learning path covers OOP Dispatch: Dynamic Binding, Single & Double Dispatch — how dispatch selects method implementations at runtime.
Recommended Reading: Object-Oriented Programming
Keine Bücher für Kategorie "objektorientierte-programmierung" gefunden.



