Hi,
Iam not able to understand why the result comes out this way,
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 c1 = new C();
A c2 = new C();
A c3 = new C();
C c4 = new C();
c1.m1(c4);
c2.m1(c4);
c3.m1(c4);
}
}
The result is "AAA";
My understanding is that, although we are assigning new C() to A, the underlying object is C, so the method m1 of C should be called, giving the result "CCC". Should'nt?
I tried removing the parameters(c4) from all the method calls (also from methods). Now the result is "CCC", the same as what I expected. Can someone explain why it happens this way.