1.IN CASE OF DYNAMIC METHOD DISPATCH WHY WE USE REFERENCE
VARIABLE,WE CAN USE THE DIFFERENT DEFINED OBJECT DIRECTLY TO
ACCESS THE DATA MEMBER AND MEMBER FUNCTION OF THAT
RESPECTIVE CLASS?WHAT IS THE MAIN FUNCTION OF "REFERENCE
VARIABLE" HERE?
Answer Posted / shweta kunjadia
It shows Runtime Polymorphism
Eg.
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
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
}
}
| Is This Answer Correct ? | 3 Yes | 1 No |
Post New Answer View All Answers
What is the purpose of encapsulation?
What is method overriding in java ?
What are the benefits of java?
Enlist diffrent types of inheritance supported by java?
Does java map allow duplicates?
How do you reverse a word in java?
How can you traverse a linked list in java?
What does provide mean construction?
What is package private scope in java?
How do you use nextline in java?
What is variable length arguments in java?
You're given a Boolean 2D matrix, can you find the number of islands?
what do you mean by marker interface in java?
What do you mean by object?
What is the difference between size and length in java?