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


Please Help Members By Posting Answers For Below Questions

When does an object becomes eligible for garbage collection in java?

573


If an object reference is set to null, will the garbage collector immediately free the memory held by that object?

635


Can the interface be final?

574


Explain heap sort?

698


How will you calculate the depth of a binary tree if the tree contains 15 nodes?

596






What are the advantages of autoboxing?

535


What is the difference between hashmap and hashtable in java?

553


What is floor in java?

556


How do you decide when to use arraylist and linkedlist?

530


How do you escape in java?

577


Differentiate between vector and array list.

634


Can we declare main () method as non static?

518


What are thread groups?

568


How do I find and replace in word?

530


How can constructor chaining be done by using the super keyword?

614