Polymorphism with instance variables [duplicate]

2019-01-11 21:09发布

This question already has an answer here:

Here are three classes that I wrote:

public class Shape {

    public int x = 0;

    public void getArea() {
        System.out.println("I don't know my area!");
    }

    public String toString() {
        return "I am a shape!";
    }

    public int getX() {
        return x;
    }
}

public class Rectangle extends Shape {

    public int x = 1;

    public int getX() {
        return x;
    }

    public void getArea() {
        System.out.println("L*W");
    }

    public String toString() {
        return "I am a rectangle!";
    }
}

public class Tester {

    public static void main(String[] args) {
        Shape s = new Shape();
        Rectangle r = new Rectangle();

        System.out.println(r);
        System.out.println(r.x + "\n");

        s = r;
        System.out.println(s);
        s.getArea();
        System.out.println(s.x);
        System.out.println(s.getX());
    }
}

The output from the main method of the Tester class is:

I am a rectangle!

1

I am a rectangle!

L*W

0

1

Why does s.x return 0 and not 1? As isn't the current instance of the variable a Rectangle and that class also has that same instance variable declared, or does the variable in the Rectangle class not override the previous public x variable in the Shape class as it does to the getX() method in the rectangle class thus returning 1?

Also as a general rule the superclass has access to the implementation of the its subclasses methods only if they are declared in that class as well? Is this because the compiler will see that the same amount of methods with the same signature are in the "Shape" class (with overridden Rectangle implementations) and accept those as valid Shape methods?

Thanks in advance,

2条回答
Ridiculous、
2楼-- · 2019-01-11 21:28

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

查看更多
beautiful°
3楼-- · 2019-01-11 21:46

Subclasses only inherit variables and methods in the superclass, not the other way around. So in order to get x to equal 1 you have to call rectangle not shape.Unless you do what the other guy demonstrated with casting which you should avoid as much as possible in real programming. Plus you should never use public instance variables ever! If you want variables to be public at least make them static or constant.

查看更多
登录 后发表回答