This is a homework question I can't figure out.
Find the output of:
public static void main(String[] args) {
Object o = new A();
A a2 = new B();
B b = new B();
a2.report(b);
}
public class A {
public void report(Object o) {
System.out.println("Object");
}
public void report(A a) {
System.out.println("A");
}
}
public class B extends A {
public void report(A a) {
System.out.println("A again");
}
public void report(B b) {
System.out.println("B");
}
}
When I tried to work the solution, I first tried to figure out what would happen at the compile time. I thought that a2 would use class A and report b which is not defined in class A and therefore would default to report object. This would give the solution of "Object". However, this is not correct. I can't figure out how this method would report "A" when the function is should be calling report(B b).