what is dynamic method dispatch ?
Answers were Sorted based on User's Feedback
Answer / khairunneesa.m
dynamic method dispatch is the mechanism by which a call to
an overridden method is resolved at runtime, rather than
compile time
Is This Answer Correct ? | 5 Yes | 3 No |
Answer / aniruddha
It is the way by which run-time polymorphism is implemented
in java.Here a call to overridden method is resolved at
run-time rather than at compile-time,determining the type of
object,passed as the reference to the overridden
method.Hence,the compiler checks the compatible method with
the given object reference(whether it is base class or
derived class method).
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / ankit
// Dynamic Method Dispatch
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
The output from the program is shown here:
Inside A's callme method
Inside B's callme method
Inside C's callme method
This program creates one superclass called A and two
subclasses of it, called B and C. Subclasses B and C
override callme( ) declared in A. Inside the main( ) method,
objects of type A, B, and C are declared. Also, a reference
of type A, called r, is declared.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / badrudiin
in cPP,virtual function is used to implement runtime
polymorphism(dynamic binding), similarly java uses dynamic
method dispach to implement dynamic binding.
Dynamic method dispatch is the mechanism by which a call to
an overridden method is resolved at run time, rather than
compile time.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / nagarjuna
dynamic method dispatcher is a runtime polymorphism which is method overriding (method overriding: same method name with different parameters)
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / md arifulla
dynamic dispatch is nothing but run time polymorphism ,
for example if we take a reference variable of super class
and the abject of sub class is assigned to the reference of
super class
ex:
class A
{
print()
{
System.out.println("class A");
}
}
class B extends A
{
print()
{
System.out.println("class B");
}
}
class C{
public static void main(String srgs[])
{
A a=new A();
B b;
b=a;
a.print();//invokes the print of class A
}
}
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / hasib reza
Determining at runtime, which overridden method to call, is called dynamic method dispatch.
This is how Java implements run-time polymorphism.
Whenever a method is called on an object reference,
Declared type of object reference is checked at compile time to make sure the method exists in declared class.
At run time, the super class objects reference could refer to an instance of any subclass of the declared reference type.
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 object being referred.
When different types of objects are referred to,
Different versions of an overridden method will be called.
It is the type of the object being referred to
Not the type of the reference variable
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / deepti sharma
Dynamic method dispatch or run time polymorphism is a property by which methods to be called are resolved at runtime.First create super class reference variable in its subclass and an object of both super and sub class and then assign the sub class object to its super class reference variable.
For ex: Class A{
void add();}
Class B extends A{
void add();}
Class C{
p.s.v.m(String args[]){
A a=new A();
B b=new C();
A r;
r=b;
r.add();//call Class B's add method
}}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / badri
i am still not clear why to use dynamic method dispatch..
because i can directly override the methods using sub class
objects..So y dynamic ..both give me same outputs and both
are at run time..????????
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / salman
In dynamic method dispatch,super class refers to subclass object and implements method overriding.
Example:
class Flower {
void which() {
System.out.println("A Beautiful flower.");
}
}
class Rose extends Flower {
void which() {
System.out.println("Rose");
}
}
class Lotus extends Flower {
void which() {
System.out.println("Lotus.");
}
}
class Test {
public static void main(String[] args) {
Flower ref1 = new Flower();
Flower ref2 = new Rose();
Flower ref3 = new Lotus();
ref1.which();
ref2.which();
ref3.which();
}
}
Is This Answer Correct ? | 0 Yes | 0 No |
Why are constructors used?
What is binary search in java?
Is java an ide?
What is collections framework?
Can interface be private in java?
What is a wrapper method?
What is supplier in java?
What is difference between array and vector?
How do you avoid global variables?
whst is encapsulation?when u encpsulate actually while devoloping code?
What is a parameter used for?
why pointer is not used in java?