The following is the code snipplet regarding my doubt.
class A {
void someMethod(A param) {
System.out.println("A");
}
}
class C extends A {
void someMethod(C param) {
System.out.println("C");
}
}
class DMD {
public static void main(String[] args) {
A ac = new C();
C c = new C();
ac.someMethod(c);
}
}
Output:
A
but I excepted the output as
C
Because I have allocated memory for C
, and A
is referring to C
's memory location, so if I call the method on the A
reference which is pointing to C
, and the argument is passed as C
type, then I expect the someMethod(C)
method should execute.
Can anyone please give me the proper reason for this behaviour?
Thanks in advance.