class G {
int x = 5;
}
class H extends G {
int x = 6;
}
public class CovariantTest {
public G getObject() {
System.out.println("g");
return new G();
}
public static void main(String[] args) {
CovariantTest c1 = new SubCovariantTest();
System.out.println(c1.getObject().x);
System.out.println(new H().x);
}
}
class SubCovariantTest extends CovariantTest {
public H getObject() {
System.out.println("h");
return new H();
}
}
Output:
h
5
6
Apparently the two println statements in the main method aren't the same. How is the new H() object returned from class SubCovariant's getObject method assigned to a G reference?