Method overriding vs overloading
Overriding replaces a parent instance method with a subclass method of the same signature. Overloading adds a method with the same name and different parameters. Override is chosen at runtime; overload is chosen at compile time.
Detailed Explanation
These words look alike and fail in opposite ways. Override is how polymorphism works. Shape.area() in a variable of type Shape runs Circle.area() when the object is a circle. The signatures match: same name, same parameter types. The return type is the same or a subtype. @Override asks the compiler to prove you hit a real parent method.
Overload is a naming convenience. print(int) and print(String) are different methods. The compiler picks one by looking at the declared types of the arguments. The runtime class of those arguments does not matter. handle(Object o) called with a String stored in an Object variable still runs handle(Object), even if handle(String) exists.
Rules differ. Override requires an instance method that is visible and not static, private, or final. Access may be widened, not narrowed. Exceptions in the throws clause may be dropped or narrowed, not widened to new checked types. Overload only needs a different parameter list. Return type alone is not enough; that does not compile.
static methods hide rather than override. Calling Parent.util() through a Child reference still uses the compile-time type if you wrote Parent.util(), and using child.util() is allowed but misleading. Private methods are invisible to subclasses, so a subclass "override" is a new method. Package-private methods only override in the same package.
You reach for overriding when behaviour varies by type and callers already have the parent handle. You reach for overloading when the same idea accepts several argument shapes (of(int), of(String)). You avoid overloading when the parameter types are siblings (Object vs String) because the wrong one is easy to select. Prefer different names (parseInt, parseIso) when the jobs differ.
The cost of mixing them is a bug that looks like a failed override. You meant to override equals(Object) and wrote equals(User). That is an overload. HashMap still calls equals(Object), and your method never runs. @Override would have caught it.
Code Examples
class Printer {
void print(Object value) {
System.out.println("object:" + value);
}
void print(String value) {
System.out.println("string:" + value);
}
}
public class OverloadPick {
public static void main(String[] args) {
Printer p = new Printer();
Object hidden = "hi";
p.print("hi"); // prints: string:hi
p.print(hidden); // prints: object:hi (declared type is Object)
}
}class Animal {
String speak() {
return "…";
}
}
class Dog extends Animal {
@Override
String speak() { // same signature: this is an override
return "woof";
}
public static void main(String[] args) {
Animal a = new Dog();
System.out.println(a.speak()); // prints: woof
}
}Common Gotchas
`equals(MyType)` does not override `equals(Object)`
it overloads. Maps and sets will ignore it. Use `@Override` on `equals(Object)`.
Overload resolution ignores runtime types
store a `String` in an `Object` and the `String` overload is not used.
`static` methods hide
they are not virtual. Qualify them with the class name.
`@Override` is cheap insurance
put it on every method you believe is an override, including interface methods.