Scrivio Interview library
ConceptIntermediate5 min read · updated 12 November 2026

Overloading is resolved at compile time and overriding at run time. Show how this causes a surprising result with equals(Order other) declared in a class.

Declaring public boolean equals(Order other) does not override Object.equals(Object); it adds an overload. The compiler chooses between overloads from the static type of the argument, so a call with an Order-typed argument uses our method, while collections and any code that holds the argument as Object call equals(Object), which is still Object's identity check. The same two orders are then equal or not depending on how the call is written, and @Override on equals makes the compiler reject the wrong signature.

Why this question is asked

This is one of the most common equals bugs in Java, and it is sneaky because the method looks right and simple tests pass. The trouble shows up later, when the objects go into a HashSet, a List.contains call or an assertion library, and equal orders are treated as different. Interviewers ask it because it connects two ideas that are often explained separately: overloading is resolved at compile time and overriding at run time. In this answer, we will recap the two mechanisms, write the broken class, run it through direct calls and collections, explain each result, and then fix it properly.

1A Quick Recap: Overloading Versus Overriding

Overriding replaces an inherited method with one that has exactly the same parameter types. The JVM chooses the implementation at run time from the object's actual class. Overloading adds another method with the same name but different parameter types. The compiler chooses among overloads at compile time, using the static types of the arguments, the types it sees in the source code.

Object declares public boolean equals(Object obj). Every collection, every hash-based structure and most libraries call exactly that method, because they only know their elements as Object. To change what equality means for our class, we must override that exact signature.

2Reproducing the Problem

Here is an Order class whose author meant to define equality by order id.

Java
class Order {
final long id;
Order(long id) { this.id = id; }
public boolean equals(Order other) { // overload, NOT an override
return other != null && id == other.id;
}
@Override public int hashCode() { return Long.hashCode(id); }
}
Order a = new Order(42);
Order b = new Order(42);
Object bAsObject = b;
System.out.println(a.equals(b)); // true: static type Order -> our overload
System.out.println(a.equals(bAsObject)); // false: static type Object -> Object.equals
System.out.println(List.of(a).contains(b)); // false: List calls equals(Object)
Set<Order> set = new HashSet<>(List.of(a, b));
System.out.println(set.size()); // 2: same hash, but equals(Object) says different

The first line looks like success, and that is how this bug survives code review and simple unit tests. Notice that the second line compares exactly the same two objects and gets the opposite answer. Nothing changed except the declared type of the argument.

3Why Each Call Gives a Different Answer

Order now has two equals methods: the one inherited from Object, equals(Object), which compares references, and our new equals(Order). When the compiler sees a.equals(b) with b declared as Order, both methods are applicable and equals(Order) is more specific, so the call is compiled to our overload. When it sees a.equals(bAsObject), the argument's static type is Object, so only equals(Object) applies, and the compiled call goes to the inherited identity check.

Run-time dispatch does not rescue us, because dispatch only chooses among overrides of the method the compiler picked. Order never overrode equals(Object), so the JVM runs Object's version. Collections are just code that holds elements as Object: ArrayList.contains calls o.equals(element) with Object-typed values, and HashSet, after finding the bucket through hashCode, calls equals(Object) too. So in every collection our overload is invisible.

Which equals runs
CallStatic type of argumentMethod chosenResult
a.equals(b)Orderequals(Order), our overloadtrue
a.equals(bAsObject)ObjectObject.equals(Object)false
list.contains(b)Object (inside ArrayList)Object.equals(Object)false
new HashSet<>(List.of(a, b))Object (inside HashMap)Object.equals(Object)size 2
Behaviour depends on the declared type, which is exactly what equals must never do.

4The Fix: Override equals(Object) and Let the Compiler Check

The correct method takes an Object, checks the type, and then compares fields. Pattern matching for instanceof makes it short.

Java
class Order {
final long id;
Order(long id) { this.id = id; }
@Override
public boolean equals(Object o) {
return o instanceof Order other && id == other.id; // false for null and other types
}
@Override public int hashCode() { return Long.hashCode(id); }
}
System.out.println(a.equals(bAsObject)); // true
System.out.println(List.of(a).contains(b)); // true
System.out.println(new HashSet<>(List.of(a, b)).size()); // 1

Now there is only one equals, it is an override, and every caller, typed or untyped, gets the same answer. The @Override annotation is the real protection. Had the original author written @Override above equals(Order other), the compiler would have refused with method does not override or implement a method from a supertype, and the bug would never have existed. Notice too that hashCode must still be overridden alongside equals, so equal objects land in the same bucket.

When the class is a simple value carrier, a record is even safer: record Order(long id) {} generates equals(Object), hashCode and toString from its components, with the right signatures.

5Follow-up questions

Should equals use instanceof or getClass()?

instanceof lets subclasses be equal to their parent, which can break symmetry if a subclass adds fields to equality. getClass() != o.getClass() makes objects equal only to the exact same class, which is safe but rejects proxies, such as Hibernate's lazy proxies, that are subclasses. For final classes and records the question disappears, which is one more reason to make value classes final.

Do IDEs or tools catch the overload?

Most do. Static analysis tools such as Error Prone and SpotBugs flag an equals method whose parameter is not Object, and IDE inspections warn too. They are a good safety net, but @Override is the simplest check because it is enforced by the compiler itself.

6Conclusion

In this answer, we saw that equals(Order other) overloads Object.equals(Object) instead of overriding it. Because overloads are chosen at compile time from static types, a call with an Order argument used our method while calls through Object, including every collection, used Object's identity check, so the same two orders were equal or not depending on how the call was written. The fix is to override equals(Object) with an instanceof check, keep hashCode consistent, and always put @Override on equals so the compiler rejects the wrong signature.

What to say in the interviewAbout 80 seconds aloud

Open with

If I declare public boolean equals taking an Order, I have not overridden Object's equals, which takes an Object. I have added an overload. Overloads are chosen by the compiler from the static type of the argument, while overrides are chosen at run time from the object's class.

Then, if they want more

So if I call a.equals(b) and b is declared as an Order, the compiler picks my overload and I get true.

But if the same b is held in a variable of type Object, the compiler can only pick equals of Object, and since I never overrode it, the JVM runs Object's version, which compares identity, and I get false for the same two objects.

Collections are exactly that case: ArrayList.contains and HashSet work with elements as Object and call equals of Object, so contains returns false and a HashSet keeps both copies even though hashCode matches. The fix is to override equals with an Object parameter, check the type with instanceof, compare the id, and keep hashCode consistent.

Most importantly, I always put @Override on equals; with it, the compiler would have rejected the Order signature immediately. For simple value classes a record generates the correct equals and hashCode for me.

About this answer

Last updated
12 November 2026
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 "Overloading is resolved at compile time and overriding at run time. Show how this causes...": https://scrivio-interview-library.netlify.app/java/oop-principles/5406a25b50/