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
What is the difference between the paint() and repaint() methods in java programming?
Give an example of call be reference significance.
Which variables are stored in heap?
Is ++ operator thread-safe in java?
What are classloaders?
What is treeset and treemap in java?
What is the base class of all classes?
what is the significance of listiterator in java?
What is array pointers ?
Does every java program need a main?
Can you extend singleton class?
Can there be an abstract method without an abstract class?
Write a program to find maximum and minimum number in array?
What is use of super keyword?
how to create constants in java?