0% found this document useful (0 votes)
6 views1 page

Java Dynamic Method Dispatch Explained

Dynamic method dispatch in Java allows overridden methods to be resolved at run time, enabling run-time polymorphism. A super class reference can point to a subclass object, and the actual method executed is determined by the object's type at the time of the call. This mechanism ensures that different versions of an overridden method are executed based on the actual object being referred to.

Uploaded by

alvahitha2000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Java Dynamic Method Dispatch Explained

Dynamic method dispatch in Java allows overridden methods to be resolved at run time, enabling run-time polymorphism. A super class reference can point to a subclass object, and the actual method executed is determined by the object's type at the time of the call. This mechanism ensures that different versions of an overridden method are executed based on the actual object being referred to.

Uploaded by

alvahitha2000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dynamic Method Dispatch

1. Method overriding forms the basis for one of Java’s most powerful concepts: dynamic
method dispatch. Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time. Dynamic method
dispatch is important because this is how Java implements run-time polymorphism.

2. Let’s begin by restating an important principle: a super class reference variable can
refer to a subclass object. Java uses this fact to resolve calls to overridden methods at
run time.

3. When an overridden method is called through a super class reference, Java


determines which version of that method to execute based upon the type of the object
being referred to at the time the call occurs. Thus, this determination is made at run time.

4. When different types of objects are referred to, different versions of an overridden
method will be called. In other words, it is the type of the object being referred to (not the
type of the reference variable) that determines which version of an overridden method
will be executed.

5. Therefore, if a super class contains a method that is overridden by a subclass, then


when different types of objects are referred to through a super class reference variable,
different versions of the method are executed.

You might also like