java covariant return type
Asked Answered
A

3

8

Why does below code prints "1" ?

class A {
    int x = 1;
}

class B extends A {
    int x = 2;
}

class Base {

    A getObject() {
        System.out.println("Base");
        return new B();
    }
}

public class CovariantReturn extends Base {

B getObject() {
   System.out.println("CovariantReturn");
   return new B(); 
}
/**
 * @param args
 */
public static void main(String[] args) {
    Base test = new CovariantReturn();
    System.out.println(test.getObject() instanceof B);
    System.out.println(test.getObject().x);
}
}
Aquarius answered 26/5, 2011 at 10:36 Comment(0)
A
13

Because you are referring to fields, which are not affected by polymorphism. If you instead used getX(), it would've returned 2.

What you are asking is, the value of field x defined in class A (because Base.getObject() returns A). Even though CovariantReturn overrides the method to return B, you are not referring to your object as CovariantReturn.

To expand a bit on how fields are not affected by polymorphism - field access is realized at compile time, so whatever the compiler sees, that's what's accessed. In your case the method defines to return A and so A.x is accessed. On the other hands methods are invoked based on the runtime type. So even if you define to return A but return an instance of B, the method you invoke will be invoked on B.

Ary answered 26/5, 2011 at 10:39 Comment(5)
but the method defines to return A. And so A.x is what is accessed.Ary
@kris you are calling method using reference of A in that it has thereturn type ACircumstance
ok, so even though test.getObject() returns an instance of B, test variable is still Base reference therefore in case where I am printing x it calls Base:getObject() ?Aquarius
test.getObject() instanceof B returns true - that is what confuses me!Aquarius
because the instanceof check happens at runtime. The A.x is realized at compile-time.Ary
E
0

@kris979 Though you are returning B, i think what makes the difference is that the return type is of A. Hence value of x in A i.e. 1 is printed.

Eosinophil answered 26/5, 2011 at 10:47 Comment(0)
S
0

As Bozho pointed out - instance variable are never affected by polymorphism. Let me give you a quick small example.

class Base {
    int i = 1;
    void method() {
        System.out.println("in base");
    }
}

class Sub extends Base {
    int i = 2;

    void method() {
        System.out.println("in sub");
    }
}

public class Test { 
    public static void main(String[] args) {
        Base obj = new Sub();
        obj.method();
        System.out.println(obj.i);
    }
}

This code will print - in sub and 1

Selfrestraint answered 8/1, 2013 at 19:16 Comment(1)
nice. simple and neat answerAdrianeadrianna

© 2022 - 2024 — McMap. All rights reserved.