Method Overloading And Overriding In Java (With example)
Method overloading and method overriding are both forms of polymorphism in Java, but they solve very different problems and are frequently mixed up by beginners.
Method Overloading
Overloading happens within the same class when multiple methods share the same name but differ in their parameter list (number, type, or order of parameters). It's resolved at compile time, which is why it's also called compile-time polymorphism.
Method Overriding
Overriding happens when a subclass provides its own implementation of a method already defined in its parent class, using the exact same method signature. It's resolved at runtime based on the actual object type, making it runtime polymorphism.
- Overloading: same class, same method name, different parameters, resolved at compile time
- Overriding: parent-child classes, identical method signature, resolved at runtime
- The @Override annotation helps catch mistakes when overriding a method incorrectly
Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.
Overloading in Code
A calculator class is the classic example. All three methods share one name, and the compiler picks the right one by looking at the arguments you pass.
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Calculator c = new Calculator();
System.out.println(c.add(2, 3)); // 5 -> add(int, int)
System.out.println(c.add(2.5, 1.5)); // 4.0 -> add(double, double)
System.out.println(c.add(1, 2, 3)); // 6 -> add(int, int, int)
The choice is fixed while compiling. If no exact match exists, Java tries widening, such as int to long, then autoboxing, then varargs, in that order.
Overriding in Code
With overriding, the decision waits until the program runs. The variable type below is Payment, but the object is a UpiPayment, and the object wins.
class Payment {
void process(double amount) {
System.out.println("Generic payment of " + amount);
}
}
class UpiPayment extends Payment {
@Override
void process(double amount) {
System.out.println("UPI payment of " + amount);
}
}
Payment p = new UpiPayment();
p.process(500); // prints: UPI payment of 500.0
This is what lets a checkout screen hold a list of different payment types and call process on each without knowing which one it is.
Calling the parent version from an override
An override does not have to throw away the parent's work. Inside the child method, super.process(amount) runs the original logic first, and the child can then add its own steps, such as logging a UPI reference number. This keeps shared behaviour in one place while each subclass adds only what is different about it.
Keep one more detail in mind: fields are never overridden. If a parent and child both declare a field with the same name, the variable type decides which one you read, which is another reason to keep fields private and expose behaviour through methods.
The Rules Side by Side
| Rule | Overloading | Overriding |
|---|---|---|
| Parameter list | Must differ | Must be identical |
| Return type | Can differ, but cannot be the only difference | Same type or a subtype (covariant) |
| Access modifier | Any | Same or less restrictive |
| Checked exceptions | Any | Same, narrower, or none |
| static, private, final methods | Can be overloaded | Cannot be overridden |
| Inheritance needed | No | Yes |
Mistakes That Compile but Misbehave
Overloading equals by accident
// Wrong: this overloads equals, it does not override it
public boolean equals(Student other) { ... }
// Right: matches Object.equals(Object)
@Override
public boolean equals(Object other) { ... }
Collections such as HashSet call equals(Object), so the first version is never used and duplicates slip in. Adding @Override turns this silent bug into a compile error.
Other traps
- Changing only the return type. Two methods named
getValue()returning int and String will not compile. - Static methods. A static method with the same signature in a child class hides the parent version. The call is chosen by the variable type, not the object.
- Ambiguous calls. Overloads such as
show(Integer)andshow(Long)makeshow(null)ambiguous and the compiler refuses it. - Narrowing access. Overriding a public method as protected causes a compile error.
Takeaway for interviews: say "overloading is decided by the compiler using the argument list, overriding is decided at runtime by the actual object", then show one small example of each.