what is dynamic method dispatch ?

Answers were Sorted based on User's Feedback



what is dynamic method dispatch ?..

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

what is dynamic method dispatch ?..

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

what is dynamic method dispatch ?..

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

what is dynamic method dispatch ?..

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

what is dynamic method dispatch ?..

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

what is dynamic method dispatch ?..

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

what is dynamic method dispatch ?..

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

what is dynamic method dispatch ?..

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

what is dynamic method dispatch ?..

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

what is dynamic method dispatch ?..

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

Post New Answer

More Core Java Interview Questions

Does garbage collection guarantee that a program will not run out of memory?

1 Answers  


What is a function argument in java?

0 Answers  


What is rmi and steps involved in developing an rmi object?

0 Answers  


What is difference between next () and nextline () in java?

0 Answers  


I want to re-reach and use an object once it has been garbage collected. How it's possible?

0 Answers  






How can we get one Interface methods whit out using implementation of interface

1 Answers   Oracle,


What is consumer interface?

0 Answers  


What is the exact difference in between unicast and multicast object? Where we will use?

0 Answers  


Why does java have two ways to create child threads? Which way is better?

0 Answers  


Can we sort hashmap in java?

0 Answers  


What is the range of the char type?

3 Answers  


Which way a developer should use for creating thread, i.e. Sub classing thread or implementing runnable.

0 Answers  


Categories