Polymorphism in OOP: One Interface, Multiple Implementations with a Worked Dispatch Trace

Learn exactly when overloaded and overridden methods are chosen. Trace a Shape array, compute two areas, and solve a Java output problem from static and runtime types.

KnowledgeGate Team

Exam prep & CS education

Updated 30 Nov 20265 min read
Polymorphism in OOP: One Interface, Multiple Implementations with a Worked Dispatch Trace

Polymorphism becomes easier when overloading, overriding, interfaces and inheritance are separated by their selection rules. In the worked Shape example, one interface routes calls to Rectangle and Circle, while a second trace shows overload selection before override dispatch. Solve each call from the reference's declared type and the object's runtime type.

Polymorphism in OOP: one contract, several valid behaviours

Polymorphism is the ability to use one operation contract while different receiver types provide behaviour suited to their own state. Here, "interface" means a callable contract. It does not mean a screen seen by a user, and it is not limited to Java's interface keyword.

Consider the language-neutral call shape.area(). Shape declares area(). Rectangle and Circle provide their own implementations. A reference declared as Shape can hold an object of either class, and dynamic dispatch selects the implementation belonging to the runtime object.

Inheritance or interface implementation establishes a subtype relationship, and substitutability lets client code accept the general type. Overriding plus dynamic dispatch makes the same call expression behave differently. The Abstract Class in OOP: How Incomplete Classes Define Interfaces article uses shared base state and incomplete subclasses. A minimal Shape interface isolates dynamic dispatch here.

Types of polymorphism: subtype, ad hoc and parametric

The three common forms vary for different reasons:

Form

What varies

Selection rule

Exact example

Subtype or inclusion polymorphism

Runtime receiver

Dispatch the override after selecting the instance-method signature

Shape s = new Circle(3); s.area() selects Circle.area()

Ad hoc polymorphism through overloading

Argument types

Choose among visible signatures at compile time

sum(2, 5) = 7 selects sum(int, int); sum("GATE", "CS") = "GATECS" selects sum(String, String)

Parametric polymorphism

Type parameter

Apply one generic algorithm to each valid type

identity<Integer>(42) = 42; identity<String>("OS") = "OS"

The exam-safe boundary is simple. Overloading is resolved from the available signatures and static argument types. For an overridden instance method, runtime dispatch begins only after a signature has been selected. Methods sharing a name do not automatically create runtime polymorphism.

"One interface, multiple implementations" describes subtype polymorphism. Ad hoc polymorphism varies visible signatures, while parametric polymorphism varies a type parameter; both use different selection rules from subtype dispatch.

Polymorphism worked example: Shape dispatch with exact areas

java
interface Shape {
    double area();
}

class Rectangle implements Shape {
    private final double width;
    private final double height;

    Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double area() {
        return width * height;
    }
}

class Circle implements Shape {
    private final double radius;

    Circle(double radius) {
        this.radius = radius;
    }

    public double area() {
        return 3.1416 * radius * radius;
    }
}

Shape first = new Rectangle(6, 4);
Shape second = new Circle(3);
Shape[] shapes = {first, second};

double total = 0.0;
for (Shape s : shapes) {
    double value = s.area();
    System.out.printf("%.2f%n", value);
    total += value;
}
System.out.printf("Total: %.2f%n", total);

Rectangle.area() gives 6 * 4 = 24.0000; Circle.area() gives 3.1416 * 3 * 3 = 28.2744. The accumulator moves 0.0000 -> 24.0000 -> 52.2744.

Array element

Static type

Runtime type

Dispatched implementation

Exact result

shapes[0]

Shape

Rectangle

Rectangle.area()

24.0000

shapes[1]

Shape

Circle

Circle.area()

28.2744

Diagram shows a Shape contract dispatching shapes[0] to Rectangle.area() for 24.0000 and shapes[1] to Circle.area() for 28.2744, totaling 52.27.

s.area() is the only call; the runtime class changes its target. Rounding occurs only for display: 24.00, 28.27 and total 52.27. The exact sum is 52.2744.

Dynamic dispatch: declared type checks access, runtime type picks the override

Method selection follows a two-stage rule. At compile time, the declared type Shape decides whether area() is a legal call and which signature is selected. At runtime, the actual Rectangle or Circle object decides which overriding body executes.

For Shape s = new Circle(3), the call s.area() is legal and returns 28.2744. Suppose diameter() is declared only in Circle. Then s.diameter() is a compile-time error even though the object is a circle, because Shape does not expose that operation. After a checked cast, Circle c = (Circle) s, the call c.diameter() may return 6. The cast changes what the compiler can see. It does not create polymorphism.

Substitutability is the practical benefit. A function such as totalArea(Shape[] items) needs only the Shape contract and works for both objects without type-specific branches. If one implementation violates that contract, its syntax may compile, but substitutability has failed. The Static vs Dynamic Binding: Compile-Time Selection and Runtime Dispatch article owns a six-output Printer trace. The interface example isolates timing to interface dispatch and overload resolution.

Polymorphism traps: overloading, hiding and missing virtual dispatch

Use this table to turn common traps into mechanical checks:

Trap

Observed result

Correction

Treating overloads as overrides

Runtime signature selection is expected

Select the overload at compile time

Adding area(int scale) but omitting area()

The Shape.area() contract remains unimplemented

Implement the exact required signature

Expecting a Shape reference to expose Circle-only methods

The compiler rejects the call

Use a Shape operation or a checked cast

Downcasting without checking the runtime type

The cast may fail

Test the object's type first

Assuming fields or static methods dispatch like overridden instance methods

The wrong target is expected

Apply runtime dispatch only to eligible overridden instance methods

Forgetting virtual on a C++ base method

The base method runs

Declare the base method virtual

In C++, given Base* p = new Derived, a virtual p->name() calls Derived::name(). A non-virtual call binds to Base::name() from the pointer's static type. Languages need not share an object layout.

Solving habit: mark declared and runtime types, resolve the overload from declared types, then dispatch the chosen overridden instance signature from the runtime receiver.

How exams test polymorphism: trace overload selection before overriding

Consider this Java-like output problem:

java
class A {
    public String f(Number x) { return "A-N"; }
}

class B extends A {
    @Override
    public String f(Number x) { return "B-N"; }
    public String f(Integer x) { return "B-I"; }
}

A ref = new B();
ref.f(Integer.valueOf(7));

A is the declared receiver, so compile time selects A.f(Number). Runtime receiver B dispatches to B.f(Number). The result is B-N, not A-N or B-I.

After the cast to B, both overloads are visible. Compile time selects the more specific B.f(Integer), which returns B-I.

Two rows trace calls from static signature selection to runtime override dispatch: A ref yields B-N, while a cast to B yields B-I.

Assessment prompts may ask for the form, output, overload versus override, base-reference legality, missing virtual, or a wrong signature. The Object-Oriented Principles and Implementation: Worked Example and Exam Traps article uses abstract Shape classes and a two-shape total. A minimal interface and a two-stage overload trace isolate the dispatch rule.

Polymorphism in OOP: the short version and next step

One stable contract, substitutable objects and overridden instance methods produce subtype polymorphism. Declared types control legal calls and overload selection. Runtime types control overridden behaviour. Check that you can reproduce both key results without looking back: 52.2744 before display rounding and B-N in the mixed overload-and-override trace.

For revision, draw the interface, write each reference's declared type above it and the runtime class below it, select the signature, then trace the dispatched body. Use Coding & Skill Development Courses to compare wider programming tracks. For structured Java and OOP practice, continue with Java Course: Concepts, MCQs & Coding Questions.