0% found this document useful (0 votes)
40 views2 pages

Dynamic Method Dispatch

Dynamic method dispatch allows a call to an overridden method to be resolved at runtime based on the object's type. In Java, this implements runtime polymorphism by determining which version of the overridden method to execute based on the object a reference refers to. Upcasting occurs when a parent class reference variable refers to a child class object. In the example, the output shows outdoor game three times because gm is changed to refer to the Cricket object, so gm.type() calls the Cricket version of type due to dynamic binding resolving the method call at runtime based on the object.

Uploaded by

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

Dynamic Method Dispatch

Dynamic method dispatch allows a call to an overridden method to be resolved at runtime based on the object's type. In Java, this implements runtime polymorphism by determining which version of the overridden method to execute based on the object a reference refers to. Upcasting occurs when a parent class reference variable refers to a child class object. In the example, the output shows outdoor game three times because gm is changed to refer to the Cricket object, so gm.type() calls the Cricket version of type due to dynamic binding resolving the method call at runtime based on the object.

Uploaded by

Kapil Dora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Dynamic method dispatch

Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at runtime. This is how java
implements runtime polymorphism. When an overridden method is called by a reference, java determines which version of that method to
execute based on the type of object it refer to. In simple words the type of object which it referred determines which version of overridden
method will be called.

Upcasting
When Parent class reference variable refers to Child class object, it is known as Upcasting

Example
class Game

public void type()

{ System.out.println("Indoor & outdoor"); }

Class Cricket extends Game

public void type()

{ System.out.println("outdoor game"); }
public static void main(String[] args)

Game gm = new Game();

Cricket ck = new Cricket();

gm.type();

ck.type();

gm=ck; //gm refers to Cricket object

gm.type(); //calls Cricket's version of type

Output:

Indoor & outdoor

Outdoor game

Outdoor game

Notice the last output. This is because of gm = ck; Now gm.type() will call Cricket version of type method. Because here gm refers to
cricket object.

Q. Difference between Static binding and Dynamic binding in java ?


Static binding in Java occurs during compile time while dynamic binding occurs during runtime. Static binding uses type(Class) information
for binding while dynamic binding uses instance of class(Object) to resolve calling of method at run-time. Overloaded methods are bonded
using static binding while overridden methods are bonded using dynamic binding at runtime.

You might also like