Hi
Please check out the following code :
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
public static void main(
String[] args) {
A a1 = new A(); B b1 = new B();
C c1 = new C(); A c2 = new C();
c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}
What is the result of attempting to compile and run the program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error
e. Run-time error
f. None of the above
Correct answer is : AAA
This is a case of overloading , so it is the reference type which determines the method to be invoked.
Now c2.m1(a1) will return A
c2.m1(b1) ?? here the reference arguement is of type B
c2.m1(c1) ?? here the refernce arguement is of type C
Thanks