29

Consider the following snippet:

import java.util.*;
public class EqualsOverload {
    public static void main(String[] args) {
        class Thing {
            final int x;
            Thing(int x)          { this.x = x; }
            public int hashCode() { return x; }

            public boolean equals(Thing other) { return this.x == other.x; }
        }
        List<Thing> myThings = Arrays.asList(new Thing(42));
        System.out.println(myThings.contains(new Thing(42))); // prints "false"
    }
}

Note that contains returns false!!! We seems to have lost our things!!

The bug, of course, is the fact that we've accidentally overloaded, instead of overridden, Object.equals(Object). If we had written class Thing as follows instead, then contains returns true as expected.

        class Thing {
            final int x;
            Thing(int x)          { this.x = x; }
            public int hashCode() { return x; }

            @Override public boolean equals(Object o) {
                return (o instanceof Thing) && (this.x == ((Thing) o).x);
            }
        }

Effective Java 2nd Edition, Item 36: Consistently use the Override annotation, uses essentially the same argument to recommend that @Override should be used consistently. This advice is good, of course, for if we had tried to declare @Override equals(Thing other) in the first snippet, our friendly little compiler would immediately point out our silly little mistake, since it's an overload, not an override.

What the book doesn't specifically cover, however, is whether overloading equals is a good idea to begin with. Essentially, there are 3 situations:

  • Overload only, no override -- ALMOST CERTAINLY WRONG!
    • This is essentially the first snippet above
  • Override only (no overload) -- one way to fix
    • This is essentially the second snippet above
  • Overload and override combo -- another way to fix

The 3rd situation is illustrated by the following snippet:

        class Thing {
            final int x;
            Thing(int x)          { this.x = x; }
            public int hashCode() { return x; }

            public boolean equals(Thing other) { return this.x == other.x; }
            @Override public boolean equals(Object o) {
                return (o instanceof Thing) && (this.equals((Thing) o));
            }
        }

Here, even though we now have 2 equals method, there is still one equality logic, and it's located in the overload. The @Override simply delegates to the overload.

So the questions are:

  • What are the pros and cons of "override only" vs "overload & override combo"?
  • Is there a justification for overloading equals, or is this almost certainly a bad practice?
Raedwald
  • 46,613
  • 43
  • 151
  • 237
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • 3
    I'd say the best practice is to use eclipse or a similar IDE to generate the equals method, making a few edits to the generated code only as necessary. – Tim Bender May 26 '10 at 07:07
  • 1
    The approach I think the best is the (Eclipse plugin) Project Lombok's @EqualsAndHashCode annotation, that auto-generate both equals and hashcode using your attributes by default (but configurable). See http://projectlombok.org/ – The Student May 26 '10 at 13:17
  • @Tim Bender: The eclipse generated code is simply terrible. You have at least to use something like Guava's `Objects.equals` to get something a bit readable. – maaartinus Aug 28 '12 at 13:19
  • @maaartinus, I don't understand your suggestion here. Guava's Objects.equal function simply relies on the Class in question to have a properly implemented equals(Object) method. For reference, read the last line of the doc after the unordered list: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Objects.html#equal(java.lang.Object,%20java.lang.Object) – Tim Bender Aug 28 '12 at 18:52
  • 1
    @Tim Bender: Compare [this generated code](http://stackoverflow.com/a/5040414/581205) and [this one](https://dl.dropbox.com/u/4971686/published/maaartin/so/EqualsAndHashCodeDemo.java). By using the one-liner `equal` you eliminate tons of null tests and make it all readable. – maaartinus Aug 28 '12 at 19:27
  • @maaartinus, I need not look at the example but only ask, did you write by hand your example? – Tim Bender Aug 28 '12 at 20:33
  • So have a look please -- it's faster to write than the generated one to read. And yes, in the few cases I don't want to use [@EqualsAndHashCode](http://projectlombok.org/features/EqualsAndHashCode.html) I write it manually. – maaartinus Aug 28 '12 at 21:02
  • https://github.com/pmd/pmd/issues/2264 – Linus Fernandes Feb 04 '20 at 23:39

8 Answers8

22

I'dont see the case for overloading equals, except that is more error-prone and harder to maintain, especially when using inheritance.

Here, it can be extremly hard to maintain reflexivity, symmetry and transitivity or to detect their inconsistencies, because you always must be aware of the actual equals method that gets invoked. Just think of a large inheritance hierarchie and only some of the types implementing their own overloading method.

So I'd say just don't do it.

b_erb
  • 20,932
  • 8
  • 55
  • 64
  • Agreed - it's more work to overload, and it gains you little. Stuff like `new Thing(0).equals("lulz")` still compiles fine, and since the cast `Thing` → `Object` is implicit it doesn't make it any "easier" to check whether two `Thing` instances `equals()` each other. – gustafc May 26 '10 at 06:49
  • 2
    I **strongly** disagree, since the overridden equals will be reflexive, transitive and symmetrical, **if and only if** the overloaded equals is. – aioobe May 26 '10 at 12:47
  • @aioobe: If `Base` adds an overloads `equals(Base)`, and overrides `equals(Object)` to call the overload, and if `Derived` overrides `equals(Object)` but does not override `equals(Base)`, and if `d1` and `d2` are both variables of type `Derived` which identify instances that differ only in ways `Base` knows nothing about, then d1.equals((Object)d2)` will invoke the `Derived` override and return `false`, but `d1.equals(d2)` will invoke the `Base` overload and return `true`. Since d2 is equivalent to itself after the cast, both comparisons with `d1` should yield matching results, but they don't. – supercat Nov 18 '13 at 20:36
  • @supercat, right. As I mention in the end of my post, it might be a good idea to make the overloaded auxiliary method private in which case this problem wouldn't arise. – aioobe Nov 18 '13 at 20:56
  • @aioobe: Alternatively, I think overriding `equals` as `final` could make anyone who wants to update the meaning of equality update the proper method, though if derived classes repeatedly override and overload, then a call to `equals(object)` could have to pass through multiple layers before reaching the actual method that implements it. – supercat Nov 18 '13 at 21:28
  • Been thinking about the approach you suggest above, and I it seems quite neat! Slightly counter intuitive both for people reading the API of the class, and it may be slightly surprising not to be able to override `equals(Object)`, but the code would look nice :-) – aioobe Nov 19 '13 at 07:08
9

If you have one single field as in your example, I think

@Override public boolean equals(Object o) {
    return (o instanceof Thing) && (this.x == ((Thing) o).x);
}

is the way to go. Anything else would be overly complicated imo. But if you add a field (and don't want to pass the 80-column recommendation by sun) it would look something like

@Override public boolean equals(Object o) {
    if (!(o instanceof Thing))
        return false;
    Thing t = (Thing) o;
    return this.x == t.x && this.y == t.y;
}

which I think is slightly uglier than

public boolean equals(Thing o) {
    return this.x == o.x && this.y == o.y;
}

@Override public boolean equals(Object o) {
    // note that you don't need this.equals().
    return (o instanceof Thing) && equals((Thing) o);
}

So my rule of thumb is basically, if need to cast it more than once in override-only, do the override-/overload-combo.


A secondary aspect is the runtime overhead. As Java performance programming, Part 2: The cost of casting explains:

Downcast operations (also called narrowing conversions in the Java Language Specification) convert an ancestor class reference to a subclass reference. This casting operation creates execution overhead, since Java requires that the cast be checked at runtime to make sure that it's valid.

By using the overload-/override-combo, the compiler will, in some cases (not all!) manage to do without the downcast.


To comment on @Snehal point, that exposing both methods possibly confuses client-side developers: Another option would be to let the overloaded equals be private. The elegance is preserved, the method can be used internally, while the interface to the client side looks as expected.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 1
    Wow, this is quite an "everything that I wanted to hear" answer! And yes, I did consider making the overload `private` for internal use to avoid downcast!! – polygenelubricants May 26 '10 at 13:10
  • Thanks. Great question / answers and discussion imo. – aioobe May 26 '10 at 13:14
  • I actually don't agree. When making the overload `private` for internal use, you can little. When making the overload `public`, you get problems like I described below. Maybe `protected` is a solution, because then you can reuse that part of code in a subclass. – Marc Jun 02 '10 at 06:41
4

Issues with Overloaded Equals:

  • All the Collections provided by Java ie; Set, List, Map use the overridden method for comparing two objects. So even if you overload the equals method, it doesn't solve the purpose of comparing two objects. Also, if you just overload and implement the hashcode method, it would result in erroneous behavior

  • If you have both overloaded and overridden equals methods and exposing both these methods you are going to confuse the client side developers. It is by convention people believe that you are overriding the Object class

Snehal
  • 7,266
  • 2
  • 32
  • 42
  • 2
    The OP himself, excluded the "just overload" option. Your second point however, is interesting. +1 for that. How would you comment on making it private then? – aioobe May 26 '10 at 12:49
3

There are a number of items in the book that cover this. (It's not in front of me, so I'll refer to items as I remember them)

There is en example exactly using equals(..) where it is said that overloading should not be used, and if used - it should be used with care. The item about method design warns against overloading methods with the same number of arguments. So - no, don't overload equals(..)

Update: From "Effective Java" (p.44)

It is acceptable to provide such a "strongly typed" equals method in addition to the normal one as long as the two methods return the same result, but there is no compelling reason to do so.

So, it is not forbidden to do so, but it adds complexity to your class, while adding no gains.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • There's an item about overloading judiciously, and also, if you're overloading, make sure that they behave compatibly among each other (i.e. don't make `take(CharSequence)` and `take(String)` do entirely different things). The compatibility is certainly present here if the forwarding pattern is enforced (which also is a recommendation in the book, i.e. to forward between one `contentEquals` overload to another). I'm very interested in an update on this answer once you've had the opportunity to revise it. – polygenelubricants May 26 '10 at 13:15
  • sure, no later than tomorrow I'll have the book in front of me and I will. (perhaps delete it? :) ) – Bozho May 26 '10 at 13:18
1

I use this approach with override and overload combo in my projects, because code looks a bit cleaner. I didn't have problems with this approach so far.

vbezhenar
  • 11,148
  • 9
  • 49
  • 63
1

Let me share an example of "buggy code" with Overloaded equals:

class A{
    private int val;

    public A(int i){
        this.val = i;
    }

    public boolean equals(A a){
        return a.val == this.val;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(this.val);
    }
}

public class TestOverloadEquals {

    public static void main(String[] args){
        A a1 = new A(1), a2 = new A(2);
        List<A> list = new ArrayList<>();
        list.add(a1);
        list.add(a2);
        A a3 =  new A(1);

        System.out.println(list.contains(a3));
    }
}
0

I can think of a very simple example where this won't work properly and why you should never do this:

class A {
   private int x;

   public A(int x) {
       this.x = x;
   }

   public boolean equals(A other) {
       return this.x == other.x;
   }

   @Override
   public boolean equals(Object other) {
       return (other instanceof A) && equals((A) other);
   }
}

class B extends A{
    private int y;

    public B(int x, int y) {
        super(x);
        this.y = y;
    }

    public boolean equals(B other) {
        return this.equals((A)other) && this.y == other.y; 
    }

    @Override
    public boolean equals(Object other) {
        return (other instanceof B) && equals((B) other);
    }
}

public class Test {
    public static void main(String[] args) {
        A a = new B(1,1);
        B b1 = new B(1,1);
        B b2 = new B(1,2);

        // This obviously returns false
        System.out.println(b1.equals(b2));
        // What should this return? true!
        System.out.println(a.equals(b2));
        // And this? Also true!
        System.out.println(b2.equals(a));
    }
}

In this test, you can clearly see that the overloaded method does more harm than good when using inheritance. In both wrong cases the more generic equals(A a) is called, because the Java compiler only knows that a is of type A and that object does not have the overloaded equals(B b) method.

Afterthought: making the overloaded equals private does solve this problem, but was does that really gain you? It only adds an extra method, which can only be called by doing a cast.

Marc
  • 3,550
  • 22
  • 28
  • 1
    The problem here has *nothing to do with overloading.* Your `equals(Object)` in class `A` is *broken itself*. Even when you inline and remove the overloads, you still get `new A(1).equals(new B(1, 2))` which is wrong, which break transitivity. Test equality of `getClass()` or [read this](http://www.artima.com/lejava/articles/equality.html). – maaartinus Aug 28 '12 at 13:34
  • The problem being demonstrated would exist even if the class tested for matching object types. If `A` overloads `equals`, then for correct operation `B` must override *that overload* of `equals`, rather than just overriding `equals(object)`. – supercat Nov 18 '13 at 20:38
0

As this question is 10 years old and the original author is probably not interested in the answer anymore, I'll add some information that can be useful to developers looking for an answer nowadays.

For Android developers, Android Studio contains a template that will pop-up when trying to overload the equal operator. It also generates a proper hashCode method, and the resulting overridden equals method would look like this:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Thing thing = (Thing) o;
    return x.equals(thing.x);
}
Pat Lee
  • 1,567
  • 1
  • 9
  • 13