Hi,
While having a mock
test, I encountered this question and still can't figure out what JVM does with this?
What is the output of the following program?
class Super { }
class Sub extends Super { }
public class Test {
public void foo(Sub sub) {
System.out.println("Sub");
}
public void foo(Super sup) {
System.out.println("Super");
}
public static void main(
String[] args) {
new Test().foo(null);
}
}
The answer here is "Sub". I looked for an explanation for the mock test but I found no explanation.
Can somebody explain the behavior of JVM regarding this? How come it's not the method foo that takes an argument of Super is called?