what is dynamic method dispatch ?

Answer Posted / j mathew

class A{
void hallo(){
System.out.println(" hallo of A");
}
}
class B extends A {
void hallo(){ // method overridden.
System.out.println("hallo of B");
}
}
class C extends B {
void hallo(){ // method overridden
System.out.println("hallo of C");
}
}
class Demo {
public static void main(String[] args) {
A a = new A(); // create object of A
B b = new B(); // create object of B
C c = new C(); // create object of C
A r; // create a reference of super class
r=a; // assign object of A to the reference var. r
r.hallo(); // it calls hallo() of class A
r=b; // super class variable can reference a
sub class object.
r.hallo(); // it calls hallo() of class B
r =c; // super class variable can ref. sub
class object.
r.hallo(); // it calls hallo() of class C
}
}

this is how dynamic dispath method works.
overridden methods are resolved at run time rather than
compile time. At compile time it doesn't know which over-
ridden method will be called.
it comes to know at run time, thats why it is called as run
time polymorphism.

Is This Answer Correct ?    6 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the restrictions imposed on method overriding?

646


What are the two main uses of volatile in Java?

697


Explain different forms of polymorphism?

751


How will you compute size of a structure?

669


What is a marker interface?

667






What is the access scope of a protected method?

640


Can a class be protected in java?

606


What is illegal identifier in java?

633


how can you catch multiple exceptions in java?

630


What is the meaning of I ++ in java?

713


What is garbage collector?

725


Is java 9 released?

619


What is meant by call by reference?

602


How many digits can a float hold?

635


What's the purpose of static methods and static variables?

684