Scrivio Interview library
DebuggingFoundations5 min read · updated 12 November 2026

A developer writes public boolean equals(Order other) in class Order and adds no annotation. HashSet<Order> still contains duplicates. What is wrong and how would @Override have helped?

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.

Java
class Order {
final long id;
Order(long id) { this.id = id; }
public boolean equals(Order other) { // no @Override
return other != null && other.id == id;
}
}
Order a = new Order(42), b = new Order(42);
System.out.println(a.equals(b)); // true: calls equals(Order)
Set<Order> set = new HashSet<>();
set.add(a);
set.add(b);
System.out.println(set.size()); // 2: duplicates
Object o = b;
System.out.println(a.equals(o)); // false: calls Object.equals(Object)

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.

Step throughWhat HashSet.add(b) actually calls
HashSetHashMapOrder b
put(b, PRESENT)
hashCode(), inherited identity hash
a different bucket from a, almost always
if same bucket: equals(Object), never equals(Order)
false: different references
new entry added, size 2

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.

Java
@Override
public boolean equals(Order other) { ... }
// error: method does not override or implement a method from a supertype

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.

Java
class Order {
final long id;
Order(long id) { this.id = id; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
return o instanceof Order other && other.id == id; // pattern matching, Java 16+
}
@Override
public int hashCode() { return Long.hashCode(id); }
}
set.add(new Order(42));
set.add(new Order(42));
System.out.println(set.size()); // 1

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.

What to say in the interviewAbout 80 seconds aloud

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.
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/