2

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?

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
pitosalas
  • 10,286
  • 12
  • 72
  • 120
  • Duplicate of: http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java – Tunaki Jan 30 '16 at 23:13

3 Answers3

4

Because then it won't override Object.equals, which is the method used by many standard library algorithms for checking equality of two objects.

Many of these algorithms can operate on objects of different types, which makes it necessary that equals can compare your object to one of any other class.

Philipp
  • 67,764
  • 9
  • 118
  • 153
1

Because that break the first rule of the overriding and become an overload of the equals() method of the Object class

Rules for method overriding:

  • The argument list should be exactly the same as that of the overridden method.
  • The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.
  • The access level cannot be more restrictive than the overridden method's access level. For example: if the superclass method is declared public then the overridding method in the sub class cannot be either private or protected.
  • Instance methods can be overridden only if they are inherited by the subclass.
  • A method declared final cannot be overridden.
  • A method declared static cannot be overridden but can be re-declared.
  • If a method cannot be inherited, then it cannot be overridden.
  • A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.
  • A subclass in a different package can only override the non-final methods declared public or protected.
Wael Sakhri
  • 397
  • 1
  • 8
  • good points. Is this another version of the Liskov Substitution principle? (https://en.wikipedia.org/wiki/Liskov_substitution_principle) – pitosalas Jan 31 '16 at 16:43
-1

You can simple use EqualsBuilder class in the Apache Commons library. https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/EqualsBuilder.html

public boolean equals(Object obj) {
    if (obj == null) { return false; }
    if (obj == this) { return true; }
    if (obj.getClass() != getClass()) {
      return false;
    }
    MyClass rhs = (MyClass) obj;
    return new EqualsBuilder()
                  .appendSuper(super.equals(obj))
                  .append(field1, rhs.field1)
                  .append(field2, rhs.field2)
                  .append(field3, rhs.field3)
                  .isEquals();
}

would be

public boolean equals(Object obj) {
    return EqualsBuilder.reflectionEquals(this, obj);
}