class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object o) { if (o instanceof Point) { if (!o.getClass().isAssignableFrom(getClass())) { //let the subclass decide if we are equal return o.equals(this); } Point p = (Point)o; return x == p.x && y == p.y; } return false; } public int hashCode() { return x*31 + y*29; } public String toString() { return ("Point: x = " + x + ", y = " + y); } }