+ 4

What is dynamic method dispatch ?

with relevant example

23rd Jan 2017, 4:35 PM
sai chander
sai chander - avatar
3 Answers
+ 12
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. ~~~~~~~~~~~~~~~~~ #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(); //calls Cricket's version of type gm=ck; //gm refers to Cricket object gm.type(); } } ~~~~~~~~~~~~~~~~~ out put : 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.
25th Jan 2017, 6:47 AM
Hassan Amr
Hassan Amr - avatar
+ 10
@sai You are welcome â˜ș
25th Jan 2017, 3:09 PM
Hassan Amr
Hassan Amr - avatar
+ 2
tq Hasan👍
25th Jan 2017, 2:37 PM
sai chander
sai chander - avatar