what is dynamic method dispatch ?
Answer Posted / ankit
// Dynamic Method Dispatch
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
The output from the program is shown here:
Inside A's callme method
Inside B's callme method
Inside C's callme method
This program creates one superclass called A and two
subclasses of it, called B and C. Subclasses B and C
override callme( ) declared in A. Inside the main( ) method,
objects of type A, B, and C are declared. Also, a reference
of type A, called r, is declared.
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
What is overriding in java?
What is the program compilation process?
How can we create a synchronized collection from given collection?
What is difference between throw and throws ?
Outline the major features of java.
What is lastindexof in java?
Can we declare an interface as final?
How do you format in java?
How big is a 64 bit float?
What does it mean to be immutable?
How to sort numbers in java without array?
How java is similar to c?
How many bytes is a char in java?
Explain the protected field modifier?
What is the arraylist in java?