Quick revision
The short answer
equals(Order other) does not override Object.equals(Object); it is a new overloaded method with a different parameter type. HashSet always calls equals(Object), which Order still inherits from Object, so it compares identity and keeps duplicates. With @Override the compiler would have rejected the method immediately, because it overrides nothing. The fix is public boolean equals(Object o) with a type check, together with a matching hashCode().
What to remember
In this answer, we saw that equals(Order) overloads rather than overrides equals(Object). HashSet, through HashMap, always calls equals(Object), so it used Object's identity comparison and, with no matching hashCode(), usually never compared the orders at all, keeping duplicates. @Override would have made the compiler reject the method, which is why teams require it on every overriding method. The fix is equals(Object) with a type check plus a matching hashCode(), or a record, and static analysis rules can catch the same mistake.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
equals(Order other) does not override Object.equals(Object); it is a new overloaded method with a different parameter type. HashSet always calls equals(Object), which Order still inherits from Object, so it compares identity and keeps duplicates. With @Override the compiler would have rejected the method immediately, because it overrides nothing. The fix is public boolean equals(Object o) with a type check, together with a matching hashCode().
Why this question is asked
This is a small bug with a big lesson. The code compiles, a unit test that calls order1.equals(order2) directly even passes, and yet every collection behaves as if equals was never written. Interviewers ask it because it tests three things at once: the difference between overloading and overriding, how HashSet decides that two elements are the same, and why teams insist on @Override. In this answer, we will reproduce the bug, see why the method is never called, fix it properly, and look at how @Override and static analysis catch it before it reaches production.
1Reproducing the Problem
Here is the class. It looks like a reasonable equals method: two orders are equal when their ids are equal.
Notice the last line. The same two objects give a different answer depending only on the static type of the argument. That is the signature of an overload, not an override.
2Overloading Versus Overriding
Overriding means replacing a parent method with one that has the same name and the same parameter types. Calls are then resolved at run time, based on the actual object, which is called dynamic dispatch. Overloading means adding another method with the same name but different parameter types. Which overload is called is decided at compile time, from the declared types of the arguments.
Object declares equals(Object). Our Order declares equals(Order). The parameter types differ, so this is an overload: Order now has two equals methods, the one it inherited from Object, which compares identity, and our new one. Code that calls equals with an argument whose declared type is Object gets the inherited one.
3Why HashSet Never Calls Our Method
HashSet is backed by a HashMap. When we add an element, it calls hashCode() to find the bucket, then compares the new element with each existing one in that bucket using key.equals(otherKey). Inside HashMap's code, the keys are of a generic type, which after compilation is just Object, so the compiler picked equals(Object) when HashMap itself was compiled, long before our class existed.
So HashMap calls Object.equals(Object) on our Order, which compares references. a and b are different objects, so they are not equal. In practice it usually does not even get that far: we did not override hashCode() either, so a and b almost certainly land in different buckets and are never compared at all.
6 messages in order. Play them, or step through them one at a time.
HashMap was compiled against equals(Object), so our equals(Order) overload is invisible to it.
4How @Override Would Have Helped
If the developer had written @Override above the method, the compiler would have checked that it really overrides something, found that no supertype has equals(Order), and refused to compile it.
The mistake would have cost a few seconds instead of a production investigation into duplicate orders. This is the main reason many teams require @Override on every overriding method and turn on a check that enforces it: it turns a silent logic bug into a compile error.
5The Correct Fix
The real equals must take Object, check the type, and compare the fields. Since equal objects must have equal hash codes, hashCode() must be overridden with the same fields.
If Order is really just data, a record does all of this for us: record Order(long id) {} gets equals, hashCode and toString generated from its components. For JPA entities the rules are more subtle, because ids may be assigned only on save, but the signature problem is the same.
6Follow-up questions
Which static analysis rule would catch this even without @Override?
SpotBugs reports it as a covariant equals method (pattern EQ_SELF_NO_OBJECT), Error Prone has a NonOverridingEquals check, and IntelliJ and Sonar have inspections for an equals method that does not take Object. Error Prone's MissingOverride check also flags any overriding method without @Override. Rule names vary between tool versions, so check the tool's documentation.
Is it ever useful to keep an equals(Order) overload next to equals(Object)?
Almost never. It creates two methods with possibly different answers, chosen by the compiler from static types, which is a source of confusion. If we want a typed comparison, a differently named method such as sameIdAs(Order) is clearer.
Would the bug appear with a TreeSet?
No, because TreeSet does not use equals at all. It uses compareTo or a Comparator to decide whether two elements are the same. A TreeSet<Order> with a comparator on id would treat both orders as one element, which shows how the contract each collection relies on differs.
7Conclusion
In this answer, we saw that equals(Order) overloads rather than overrides equals(Object). HashSet, through HashMap, always calls equals(Object), so it used Object's identity comparison and, with no matching hashCode(), usually never compared the orders at all, keeping duplicates. @Override would have made the compiler reject the method, which is why teams require it on every overriding method. The fix is equals(Object) with a type check plus a matching hashCode(), or a record, and static analysis rules can catch the same mistake.
Open with
The method equals(Order other) does not override Object's equals, because the parameter type is different. It is an overload, so the class now has two equals methods: mine and the inherited equals(Object), which compares identity.
Then, if they want more
Overloads are chosen at compile time from the declared argument type, and HashSet's backing HashMap was compiled against equals(Object), so it never calls my method. It uses identity, and since hashCode was not overridden either, two equal orders usually land in different buckets anyway, so the set keeps duplicates.
You can even see it in a test: a.equals(b) returns true, but a.equals((Object) b) returns false. If the developer had written @Override, the compiler would have refused to compile the method, because it does not override anything in a supertype.
That is exactly why teams require @Override on every overriding method: it turns a silent logic bug into a compile error. The fix is to write public boolean equals(Object o) with a type check, for example o instanceof Order other and then compare the ids, and to override hashCode with the same field.
If the class is just data, a record generates both correctly. Static analysis catches it too: SpotBugs flags a covariant equals, and Error Prone has NonOverridingEquals and MissingOverride.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A developer writes public boolean equals(Order other) in class Order and adds no...": https://scrivio-interview-library.netlify.app/java/annotations/070803efb2/