I've seen this:
// Returns whether o refers to a Point object with
// the same (x, y) coordinates as this Point object
public boolean equals(Object o) {
if (o instanceof Point) {
Point other = (Point) o;
return x == other.x && y == other.y;
} else {
return false;
}
}
Why isn't it more sensible (and simpler) to require that the parameter of the equals method take a Point
object. Then, if we try to compare a Point with a non-Point then the compiler will catch it. Isn't that better?