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
How can you read an integer value from the keyword when the application is runtime in java? example?
What is a singleton class in Java?
What is java ceil?
What is a class component?
What is a void method?
What is a classloader in java?
What are the different collection views provided by maps?
What is the difference between a field variable and a local variable?
Can you start a thread twice in Java?
Write a program to check for a prime number in java?
What are the different ways of creating thread?
Is string pool garbage collected?
What is the purpose of using java.lang.class class?
Convert a BST into a DLL and DLL to BST in place.
What is a boolean field?