Skip to content
IRC-CodingIRC-Coding
PolymorphismDynamic BindingMethod OverloadingGenericsDispatchAlgorithmsFundamentals

OOP Polymorphism: Basics, Dynamic Binding & Dispatch

Learn polymorphism for unified calls on different implementations. Dynamic binding, overloading, generics & Open Closed Principle.

S

schutzgeist

3 min read
OOP Polymorphism: Basics, Dynamic Binding & Dispatch

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

  1. Static Binding: Method call resolved at compile time
  2. Dynamic Binding: Method call resolved at runtime
  3. Virtual Method Table (vtable): Dispatch table for polymorphic calls
  4. Method Overriding: Redefining a method in a subclass
  5. Method Overloading: Multiple methods with the same name
  6. Generics: Type-independent programming
  7. Interface Polymorphism: Different implementations of the same interface
  8. 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

  1. What’s the difference between overloading and overriding? Overloading: same method name, different parameters. Overriding: a subclass method replaces the base class method.

  2. How does dynamic binding work? At runtime, the vtable determines which method to call based on the object’s actual type.

  3. 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.

  4. Why does polymorphism support the Open Closed Principle? New functionality can be added through new types without changing existing code.

Key Sources

  1. https://en.wikipedia.org/wiki/Polymorphism_(computer_science)
  2. https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
  3. 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.

Keine Bücher für Kategorie "objektorientierte-programmierung" gefunden.

Back to Blog
Share:

Related Posts