What rules must an overriding method follow in Java, and why should you always write @Override?
Show the explanation
An overriding method must:
- Have the same name and parameter types as the parent method.
- Have the same return type or a subtype (covariant return).
- Not be less accessible (a
publicmethod cannot becomeprotected). - Not throw new or broader checked exceptions (fewer or narrower is fine; unchecked is unrestricted).
- Not override a
final,staticorprivatemethod (static methods are hidden instead; private ones are not inherited).
@Override asks the compiler to check that you really are overriding. Without it, a small mistake silently creates an overload instead:
class Point {
int x, y;
// Bug: equals(Point) overloads equals(Object); HashSet and List.contains will not call it
public boolean equals(Point p) { return x == p.x && y == p.y; }
}
// With @Override on that method, the compiler reports an error immediately.The correct signature is @Override public boolean equals(Object o), and it should always come with a matching hashCode().
Original UniCircle question. Last reviewed 22 Oct 2026.
