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

Which variables are stored in heap?

541


What happens if we override only equals?

580


What are the types of literals?

559


What is parameters example?

554


Can you explain the final method modifier?

567






Is 0 an even number?

548


How many bytes is string in java?

607


What is files manifesting?

1727


What are the two categories of data types in the java programming language?

507


how to handle exceptions in ejb?

1873


Why pass by reference is not possible in java?

494


What is the purpose of object oriented programming?

538


what is synchronization and why is it important? : Java thread

586


Does treeset use compareto?

541


Explain the usage of this with constructors?

528