Polymorphism with instance variables [duplicate]

There is no polymorphism for fields in Java. There is however, inheritance. What you’ve effectively done is create two fields in your Rectangle class, with the same name. The names of the field are, effectively:

public class Rectangle {
    public int Shape.x;
    public int Rectangle.x;
}

The above doesn’t represent valid Java, its just an illustration of how the fields are scoped in your class

Within the entire scope of the Rectangle class, the superclass field of the same name is hidden. So anytime you reference the simple name x, or the scoped name this.x, within the class, you are referring to the field that is defined in Rectangle. You can actually access the superclass field as well, with the scoped name super.x.

Now, from outside of the class, the rules for which field is being accessed is slightly different. The scope will be determined by the compile time type of the class that the field is being referenced from. So in your code:

Shape s = new Shape();
Rectangle r = new Rectangle();

s = r;
System.out.println(s.x);

The output is 0 because the compile time type of s is Shape (not Rectangle). You can observe a change in this behavior when you do this:

Shape s = new Shape();
Rectangle r = new Rectangle();

s = r;
System.out.println(((Rectangle)s).x);

Presto! Your output is now 1, because the compiler sees that you’ve scoped the field access to Rectangle.

To condense the rules of visibility:

You can read more about instance variable hiding in the JLS, Section 8.3.3.2

Leave a Comment