class HelloWorld{
public static void main(String []args){
A j = new B();
B k = new B();
System.out.println(j.foo(k));
}
}
class A {
public int foo(A p) { return 1; }
}
class B extends A {
public int foo(A p) { return 2; }
public int foo(B p) { return 3; }
}
So the output printed would be 2. I don't understand how this is so. Would the j
object not be seen as its compiled form: A
? Yet it executes the class B
version of foo
using an A
type parameter even though k
is of class B
at both compile and run time. Why is this so?