Dynamic method Dispatch

What is dynamic method dispatch?

Questions by ganesh_tot

Showing Answers 1 - 13 of 13 Answers

This is a unique feature in JAVA.as the name itself suggests that it is related with run time.In this classes has methods with same method name and signature.However which method will b executed will be decided during run time.

  Was this answer useful?  Yes

salil.kaul

  • Apr 11th, 2006
 

Consider this piece of code: 2 classes one derived from another with methods named "method" with same name and signatureclass base

Code
  1.  

  2. {  

  3. void method()

  4. {          

  5. System.out.println("I am in base methodn");  

  6. }

  7. }

  8. class derived extends base

  9. {  

  10. void method()

  11. {          

  12. System.out.println("I am in derived methodn");  

  13. }

  14. }

  15. public static void main(String args[])

  16. {    

  17. base b;             // Reference to base class    

  18. b = new base();    

  19. b.method();    // Now point the reference to object of derived    

  20. b = new derived();      

  21. b.method();  // calls derived overridden method because object pointed is of type derived

  22. }

  23. Output:I am in base method I am in derived method

  24.  

  Was this answer useful?  Yes

mastanreddy

  • Oct 27th, 2006
 

Hi
This is Mastan
dynamic method dispatch is run time polymorphism , superclass reference variable can refer to subclass object

Code
  1. public class sum1

  2. {

  3.     int a;

  4.     int b;

  5.     void m1()

  6.     {

  7.         System.out.priontln("a"+"b");

  8.     }

  9. }

  10. public class sum2 extends sum1

  11. {

  12.     int c;

  13.     void m2()

  14.     {

  15.         System.out.println(c);

  16.     }

  17. }

  18. public static void main(String args[])

  19. {

  20.     sum1 s=new sum2();

  21.     s.m1();

  22.     s.m2();

  23. }

  24. }

  25.  

  Was this answer useful?  Yes

Lalit

  • Nov 30th, 2006
 

Hi,

I think sub class can instantiate super class instance but can not give right to access its member.

i.e.

sum1 s=new sum2();

s.m1();// OK

s.m2();// Not rigjht because m2 is a member of sum2 class . If i am wrong then please give clerification

Thanks

  Was this answer useful?  Yes

pranab mohanty

  • Feb 12th, 2007
 

Dynamic method dispatch is a technique by which a call to a overridden method is solved at runtime rather than compile time..this is how java implements runtime polymorphism.

  Was this answer useful?  Yes

srinu.p

  • Apr 24th, 2008
 

Hi
My name is srinivas.Iam very much confused about DYNAMIC METHOD DISPATCH.
Can any one help me with a clear example.

  Was this answer useful?  Yes

Dynamic method dispatch is run time polymorphism/late binding. When you override a super class method in subclass, the method which is to be  executed is decided at the run time rather than at the compile time.

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

arun

  • Dec 29th, 2011
 

According to my view in dynamic method dispatching both super class and sub class have same method so in this method in super class dispatches, base class method executed than super class method executed.

  Was this answer useful?  Yes

Shahhid Hussain

  • Jan 2nd, 2012
 

Code
  1. class A {

  2.   void callme() {

  3.     System.out.println("Inside As callme method");

  4.   }

  5. }

  6.  

  7. class B extends A {

  8.   void callme() {

  9.     System.out.println("Inside Bs callme method");

  10.   }

  11. }

  12.  

  13. class C extends A {

  14.   void callme() {

  15.     System.out.println("Inside Cs callme method");

  16.   }

  17. }

  18.  

  19. class Dispatch {

  20.   public static void main(String args[]) {

  21.     A a = new A(); // object of type A

  22.     B b = new B(); // object of type B

  23.     C c = new C(); // object of type C

  24.     A r; // obtain a reference of type A

  25.  

  26.     r = a; // r refers to an A object

  27.     r.callme(); // calls As version of callme

  28.  

  29.     r = b; // r refers to a B object

  30.     r.callme(); // calls Bs version of callme

  31.  

  32.     r = c; // r refers to a C object

  33.     r.callme(); // calls Cs version of callme

  34.   }

  35. }

  Was this answer useful?  Yes

Bismay kumar Biswal

  • Feb 29th, 2012
 

Dynamic dispatch method is achieved through run time polymorphism.when a method of base class is being overridden by their corresponding child class,the object of that class make the call to their corresponding and it is decided at the run time when object is created though it may be referenced by base class variable

  Was this answer useful?  Yes

saroj kumar

  • Apr 9th, 2012
 

Hi,
The process of assigning sub class object to super class reference variable is called as Dynamic Dispatch.
Consider the following classes,

Code
  1. class X{}

  2. class Y extends X{}

  3.  

  4. X obj1=new Y();  -> Dynamic Dispatch

  Was this answer useful?  Yes

zeeshan

  • Jun 12th, 2012
 


Hi Friend,
In dynamic method dispatch,super class refers to subclass object and implements method overriding.

Code
  1. Example:

  2.  

  3. class Flower {

  4. void which() {

  5. System.out.println("A Beautiful flower.");

  6. }

  7. }

  8. class Rose extends Flower {

  9. void which() {

  10. System.out.println("Rose");

  11. }

  12. }

  13. class Lotus extends Flower {

  14. void which() {

  15. System.out.println("Lotus.");

  16. }

  17. }

  18. class Test {

  19.  

  20. public static void main(String[] args) {

  21. Flower ref1 = new Flower();

  22. Flower ref2 = new Rose();

  23. Flower ref3 = new Lotus();

  24. ref1.which();

  25. ref2.which();

  26. ref3.which();

  27. }

  28. }

  29.  

  30.  

  31.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions