Dynamic Binding in OOP: Runtime Method Resolution with Worked Examples

Separate a reference's declared type from an object's runtime type. Trace Java overrides, overloads and hidden members, then compare C++ virtual dispatch.

KnowledgeGate Team

Exam prep & CS education

Updated 20 Nov 20265 min read
Dynamic Binding in OOP: Runtime Method Resolution with Worked Examples

Saying “the method is chosen at runtime” is incomplete. You must first separate a variable's declared type from the object's actual type, or overloads, fields and static methods will produce surprising answers. Separate declared types, runtime types, overloads, fields and static methods to predict each result.

Dynamic binding in OOP starts with two types

Dynamic binding, also called late binding or dynamic dispatch, chooses the implementation of an eligible overridden instance method from the receiver object's runtime class. “Eligible” matters because fields, static methods and non-virtual C++ methods follow other rules.

Consider Alert ref = new SmsAlert();. The declared, or static, type of ref is Alert. The runtime, or dynamic, type of the object is SmsAlert.

At compile time, Java permits only operations available through Alert. At runtime, an overridden implementation in SmsAlert may execute. If only SmsAlert declares carrierName(), then ref.carrierName() does not compile, despite the runtime object being an SmsAlert. Calling it requires an explicit valid downcast, such as ((SmsAlert) ref).carrierName().

The Object Oriented Technology Explained guide provides the four-pillar foundation, class-object distinctions and interface choices. The Alert trace below isolates runtime override selection.

Runtime method resolution follows a fixed order

Trace ref.cost(4) in four steps:

  1. The compiler confirms that Alert declares an accessible cost(int).

  2. At runtime, Java checks that the receiver is not null.

  3. The receiver's actual class is identified as SmsAlert.

  4. The most specific applicable override, SmsAlert.cost(int), executes.

In the Alert hierarchy, abstract Alert declares channel() and cost(int messages).

With Alert ref = new SmsAlert();, ref.channel() returns SMS. The call ref.cost(4) first satisfies the Alert.cost(int) contract, then dispatches to SmsAlert.cost(int). It calculates 4 * 3 = 12 and returns 12.

A virtual-method table is a useful conceptual model for this lookup, but a language or virtual machine specification need not require that exact physical layout. For the wider binding taxonomy, Static vs Dynamic Binding follows scope, aliasing, fields, hidden static methods and overload selection through one six-output Java program. Receiver dispatch needs a different trace: four lookup steps, one call across several runtime classes, then the Java/C++ virtual-method boundary.

Dispatch trace: an Alert reference to an SmsAlert object runs SmsAlert.cost(int), computing 4 x 3 = 12.

One call site can behave polymorphically

Now put different objects behind references with one declared type:

java
Alert[] batch = {
    new EmailAlert(), new SmsAlert(), new PushAlert()
};

int total = 0;
for (Alert alert : batch) {
    total += alert.cost(4);
}

The loop contains the identical source-level call each time, yet dispatch follows each receiver:

Runtime object

Selected implementation

Calculation

Accumulator

EmailAlert

EmailAlert.cost(4)

4 * 2 = 8

0 -> 8

SmsAlert

SmsAlert.cost(4)

4 * 3 = 12

8 -> 20

PushAlert

PushAlert.cost(4)

4 * 1 = 4

20 -> 24

The final total is 24. The loop depends on the stable Alert contract and needs no if (type == ...) chain. A later InAppAlert can join the array if it supplies the same operations. After tracing this output, use the Java Course: Concepts, MCQs and Coding Questions for language practice.

Dispatch table for alert.cost(4): EmailAlert gives 8, SmsAlert 12, PushAlert 4, running total 24.

Dynamic binding differs from fields and static methods

Java does not dispatch every member dynamically. Compare these member-access rules:

java
class BaseNotice {
    String label = "BASE_FIELD";
    static String source() { return "BASE_STATIC"; }
    String code() { return "BASE_METHOD"; }
}

class SmsNotice extends BaseNotice {
    String label = "SMS_FIELD";
    static String source() { return "SMS_STATIC"; }
    @Override String code() { return "SMS_METHOD"; }
}

For BaseNotice x = new SmsNotice();, the results are different for clear reasons:

  • x.label is BASE_FIELD because field access follows the declared type.

  • x.source() is BASE_STATIC because static methods are hidden and resolved from the reference or class context. Prefer the clearer BaseNotice.source() form.

  • x.code() is SMS_METHOD because an eligible overridden instance method is dynamically dispatched.

Private methods are not overridden, final methods cannot be overridden, constructors are not inherited, and fields are not polymorphic. The mnemonic “objects decide methods, references decide fields” applies to this Java example, not as a universal law across languages.

Overloading and overriding use different decision times

Overloading selects among parameter lists at compile time using declared expression types. Overriding selects the runtime implementation of the signature already chosen.

Suppose Formatter declares format(Number n), returning "base-number:" + n, and format(Integer n), returning "base-integer:" + n. SmartFormatter overrides only format(Number n) and returns "smart-number:" + n.

Now create:

java
Formatter f = new SmartFormatter();
Number n = Integer.valueOf(7);

For f.format(n), compile-time overloading chooses format(Number) because n is declared Number. Runtime overriding then chooses SmartFormatter.format(Number), producing smart-number:7.

For f.format(Integer.valueOf(7)), compile-time overloading chooses format(Integer). SmartFormatter does not override that signature, so inherited Formatter.format(Integer) runs and produces base-integer:7. If you can explain both outputs, you have separated the two decision stages correctly.

Dynamic binding rules depend on the language

Java uses dynamic dispatch for overridable instance methods, while its static methods are hidden. C++ requires an explicit virtual member for dynamic dispatch through a base pointer or reference. An ordinary C++ member remains statically bound.

Let Base::score() be virtual and return 5, while Base::fixed() is non-virtual and returns 1. Let Derived::score() return 9 and Derived::fixed() return 2. With Base* p = new Derived;, p->score() returns 9 through virtual dispatch, but p->fixed() returns 1 through static binding.

Dynamic binding here means runtime receiver-method resolution. It does not mean that the language is dynamically typed. Java is statically typed and still performs dynamic method dispatch.

Dynamic binding exam patterns and trace traps

Four question forms recur in exams: predict output from a base reference and derived object, classify a member as overloaded, overridden, hidden or non-virtual, separate declared type from runtime type, and compare Java instance dispatch with C++ virtual dispatch.

Use this five-line drill. For every line, write both the compile-time selected signature and the runtime selected implementation:

  • ref.cost(4) -> 12

  • three-object batch total -> 24

  • x.label -> BASE_FIELD

  • f.format(n) -> smart-number:7

  • C++ p->fixed() -> 1

There is also a null-receiver boundary. With Alert ref = null, ref.cost(4) passes compile-time member lookup but throws NullPointerException before any implementation runs.

KnowledgeGate has more than ten practice questions on this exact subtopic. Use the GATE CS exam preparation category for broader practice across the rest of the subject.

Dynamic binding: the short version and next step

Ask two questions in order. First, “Which signature is legal and selected from the declared types?” Second, “Which eligible override belongs to the runtime receiver?” For Alert ref = new SmsAlert(); ref.cost(4), the contract is Alert.cost(int), the implementation is SmsAlert.cost(int), and the result is 12.

Redraw the three-object batch, cover the implementation column, then predict 8, 12, 4 and total 24. Replace the middle object with new PushAlert() and recompute without changing the loop: 8 + 4 + 4 = 16.

The Dynamic Binding MCQs: 8 Solved Questions and 4 Runtime Dispatch Drills provide the question-set practice. On every call, trace declared type, selected signature, runtime type, chosen implementation and final value in that order.