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 is a class instance variable?

612


What is byte value?

549


What is a control variable example?

524


Can we declare main () method as non static?

512


Why wait(),notify(),notifyAll() methods defined in Object class althought we are using in only threads.

2362






Can array grow dynamically in java?

514


What is the role of garbage collector in java?

483


Why is core java important?

568


When is the garbage collection used in Java?

649


What technique is carried out to find out if a particular string is empty?

557


Why declare Main() method as a static in java ?

591


Realized?

1650


What is integer valueof?

633


What is operator overloading. Is it is supported in java?

513


How concurrent hashmap works?

600