Java Q&A
by Tal Cohen

Listing One
class Point {
    private int x;
    private int y;
    // (obvious constructor omitted...)
    public boolean equals(Object o) {
        if (!(o instanceof Point))
            return false;
        Point p = (Point)o;
        return (p.x == this.x && p.y == this.y);
    }
}
Listing Two
class ColorPoint extends Point {
    private Color c;
    // (obvious constructor omitted...)
    public boolean equals(Object o) {
        if (!(o instanceof ColorPoint))
            return false;
        ColorPoint cp = (ColorPoint)o;
        return (super.equals(cp) && cp.color == this.color);
    }
}
Listing Three
class ColorPoint extends Point {
    private Color c;
    // (obvious constructor omitted...)
    public boolean equals(Object o) {
        if (!(o instanceof Point))
            return false;
        // if o is a normal Point, do a color-blind comparison:
        if (!(o instanceof ColorPoint))
            return o.equals(this);
        // o is a ColorPoint; do a full comparison:
        ColorPoint cp = (ColorPoint)o;
        return (super.equals(cp) && cp.color == this.color);
    }
}
Listing Four
class ColorPoint {
    private Point point;
    private Color color;
    // ...etc.
}
Listing Five
class Point {
    private int x;
    private int y;
    protected boolean blindlyEquals(Object o) {
        if (!(o instanceof Point))
            return false;
        Point p = (Point)o;
        return (p.x == this.x && p.y == this.y);
    }
    public boolean equals(Object o) {
        return (this.blindlyEquals(o) && o.blindlyEquals(this));
    }
}
Listing Six
class ColorPoint extends Point {
    private Color c;
    protected boolean blindlyEquals(Object o) {
        if (!(o instanceof ColorPoint))
            return false;
        ColorPoint cp = (ColorPoint)o;
        return (super.blindlyEquals(cp) &&
        cp.color == this.color);
    }
}






