Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Dynamic method invocation in Java is the runtime selection of an overridden implementation for an eligible instance-method call. The compiler first resolves which method signature the call refers to; at runtime, Java uses the target object’s actual class to select the matching implementation. This is why a superclass or interface reference can invoke a subclass’s override. The phrase also has a separate meaning in distributed systems, where a request can be routed to a service implementation discovered at runtime.

How does dynamic method dispatch work in Java?

A Java method call involves distinct decisions. Compile-time resolution identifies the method descriptor—the method name, parameter types, and return type relevant to the invocation. For an eligible virtual or interface call, runtime lookup then selects an implementation for that descriptor based on the target object’s runtime class. The Java Language Specification describes the language behavior, not a required sequence of visible searches: implementations may use structures such as per-class dispatch tables to carry it out efficiently. Oracle’s Java SE 20 Language Specification, Chapter 15 defines invocation expressions and dynamic lookup.

A superclass reference can reach a subclass override

Suppose Animal declares an instance method named speak(), and Dog extends Animal and overrides that method. If a variable declared as Animal refers to a Dog object, calling speak() can run Dog’s implementation. The declared type helps determine which method signature the call invokes; the object’s runtime class determines the overriding implementation for that eligible call. This is the core polymorphic behavior known as dynamic dispatch. The National University of Singapore’s method-invocation teaching material explains the compile-time descriptor and runtime implementation distinction.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How is dynamic dispatch different from overloading?

Overloading and overriding happen at different stages. Overloading provides multiple methods with the same name but different parameter lists; compile-time resolution chooses the applicable signature using the call context and argument types. Overriding replaces an inherited instance-method implementation in a subclass; runtime dispatch can select that override for an eligible call. Java does not choose among every overload anew just because the target object has a different runtime class.

  • Overloading: Which signature does this call mean? Resolved at compile time.
  • Overriding: Which class implementation supplies that resolved instance method? Selected at runtime when the invocation mode supports dynamic lookup.

Which Java calls use dynamic method invocation?

The phrase is most useful for polymorphic calls to overridden instance methods. It does not describe every Java method call in the same way. In particular, static methods are not dispatched as overridden instance methods, and a super invocation follows a distinct lookup rule rather than starting from the target object’s runtime class as an ordinary virtual or interface call does. The exact rules depend on the invocation mode in the Java Language Specification.

Does reflection use dynamic method invocation?

Reflection is an API for invoking a method represented by a Method object; it is not another name for dynamic dispatch in general. For an instance method, however, overriding still matters: Android’s Method.invoke reference states that invocation on a target object uses overriding based on that object’s runtime type. Thus reflection changes how the method is identified and invoked, but it does not necessarily bypass ordinary instance-method dispatch.

What does the phrase mean in distributed systems?

In some systems-programming contexts, “dynamic method invocation” refers to a client asking for a service by its description without knowing the particular object identifier or class that will handle it. At runtime, the system discovers a suitable service implementation and routes the request. Here the lookup concerns a service or object in a distributed architecture, not Java’s selection of an overridden method within an object hierarchy. A ScienceDirect Topics page reproduces this usage from Richard John Anthony’s Systems Programming (2016).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How does this compare with .NET reflection?

The broad idea of runtime lookup also appears in other language platforms, but the rules are language-specific. Microsoft’s documentation describes reflection invocation of virtual methods and late binding in .NET. Those terms are related to runtime method selection, but they should not be treated as interchangeable with Java’s precise invocation rules. See Microsoft’s MethodBase.Invoke reference and guide to dynamically loading and using types.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.