Java Covariants

后端 未结 7 820
臣服心动
臣服心动 2021-01-01 22:26
public class CovariantTest {
    public A getObj() {
        return new A();
    }

    public static void main(String[] args) {
        CovariantTest c = new SubCov         


        
7条回答
  •  [愿得一人]
    2021-01-01 22:57

    This is because in Java member variables don't override, they shadow (unlike methods). Both A and B have a variable x. Since c is declared to be of type CovarientTest, the return of getObj() is implicitly an A, not B, so you get A's x, not B's x.

提交回复
热议问题