OOP Polymorphism: Fundamentals, Dynamic Binding & Dispatch
This post is a term explanation for polymorphism in object-oriented programming – including exam questions and tags.
In a Nutshell
Polymorphism enables a single call to be applied to different concrete implementations; the selection of the appropriate method occurs context-dependently, typically at runtime. Result: flexible, extensible software with lower coupling to concrete types.
Compact Technical Description
Polymorphism refers to the ability of objects to respond differently to the same message, depending on their actual type. The core is dynamic binding; method calls are resolved at runtime via dispatch tables (vtable) to the appropriate implementation.
We distinguish between:
- Subtype polymorphism via interfaces and inheritance
- Parametric polymorphism via generics
- Ad-hoc polymorphism via overloading
Polymorphic design supports the Open Closed Principle: Extensions occur through new types rather than modifying existing ones. A prerequisite is a stable contract; signatures and semantics must remain compatible (Liskov Substitution Principle).
In dynamically typed languages, similar effects arise through duck typing, with focus on available operations rather than static type relationships.
Exam-Relevant Key Points
- Dynamic binding at runtime via vtable
- Subtype polymorphism via inheritance and interfaces
- Method overloading as ad-hoc polymorphism
- Generics enable parametric polymorphism
- Open Closed Principle through polymorphic design
- Liskov Substitution Principle as a prerequisite
- Duck typing in dynamic languages
- Dispatch mechanisms for method resolution
Core Components
- Static binding: Method call fixed at compile time
- Dynamic binding: Method call resolved at runtime
- Virtual Method Table (vtable): Dispatch table for polymorphic calls
- Method overriding: Overwriting in subclasses
- Method overloading: Multiple methods with the same name
- Generics: Type-independent programming
- Interface polymorphism: Different implementations of an 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: New implementations without code changes
- Maintainability: Reduced coupling between components
- Reusability: Generic algorithms and data structures
Disadvantages
- Performance overhead: Dynamic binding costs time
- Complexity: Harder to understand and debug
- Runtime errors: Type problems detected only at runtime
- Memory consumption: vtable and additional objects
Common Exam Questions
-
What is the difference between overloading and overriding? Overloading: Same method name, different parameters. Overriding: Method in subclass overrides base class method.
-
How does dynamic binding work? At runtime, the appropriate method is called via the vtable based on the actual object type.
-
What is duck typing? “If it walks like a duck and quacks, 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 modifying existing code.
Most Important 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
Recommended Reading: Object-Oriented Programming
Keine Bücher für Kategorie "objektorientierte-programmierung" gefunden.