Given the classes:
class Pupil {
String name = “unknown”;
public String getName() {return name;}
}
class John extends Pupil {
String name = “John”;
}
class Harry extends Pupil {
String name = “Harry”;
public String getName() {return name;}
}
public class Director {
public static void main(String[] args) {
Pupil p1 = new John();
Pupil p2 = new Harry();
System.out.print(p1.getName() + ” “);
System.out.print(p2.getName());
}
}
What is the result?
A.
John Harry
B.
unknown Harry
C.
john unknown
D.
unknown unknown
E.
Compilation fails.
F.
An exception is thrown at runtime.
B
B
B
In Harry there is an overridden method and therefore the name in the Harry instance is used.
Jhon only declared a new name but without the overridden method this variable is never gonna used.