This question already has an answer here:
- Overriding member variables in Java 10 answers
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,
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:
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 namethis.x
, within the class, you are referring to the field that is defined inRectangle
. You can actually access the superclass field as well, with the scoped namesuper.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:
The output is
0
because the compile time type ofs
isShape
(notRectangle
). You can observe a change in this behavior when you do this:Presto! Your output is now
1
, because the compiler sees that you've scoped the field access toRectangle
.To condense the rules of visibility:
You can read more about instance variable hiding in the JLS, Section 8.3.3.2
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.