0

Why can I access a variable with private access without a getter (colour == ((ColouredPoint) other).colour)? Here is the code for my ColouredPoint class:

public class ColouredPoint extends Point {
    private Colour colour;

    public ColouredPoint(double x, double y, double z, Colour colour) {
        super(x, y, z);
        this.colour = colour;
    }

    @Override
    public String toString() {
        return "(" + super.toString() + ", " + colour + ")";
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof ColouredPoint) {
            return x == ((ColouredPoint) other).x
                    && y == ((ColouredPoint) other).y
                    && z == ((ColouredPoint) other).z
                    && colour == ((ColouredPoint) other).colour;
        }
        return false;
    }
}
Jeni
  • 31
  • 2
  • 4
    It's not private **within** its own class. – Hovercraft Full Of Eels Apr 07 '17 at 19:11
  • The access is restricted on the class level, not the object level. In other words, within the class, access to private fields is allowed, no matter if they belong to another object. This may be surprising, but simplifies the rule. If not, what should be the rule for static fields? They belong to no object at all. – Ole V.V. Apr 07 '17 at 20:14

0 Answers0