Method Overloading vs Overriding in Java
In this lesson: Difference between method overloading and overriding in Java — rules, comparison table and examples with output. In Hindi & English.
Two similar-sounding ideas
Overloading and overriding sound alike but are very different. Overloading is about having several methods with the same name in one class (different parameters). Overriding is about a child class replacing a parent's method. Both are forms of polymorphism.
Method overloading
class Calc {
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; }
}
// use:
Calc c = new Calc();
System.out.println(c.add(2, 3)); // 5
System.out.println(c.add(2.5, 1.5)); // 4.0
System.out.println(c.add(1, 2, 3)); // 6
4.0
6
Three methods share the name add but differ in parameters. Java picks the matching one from the arguments — this is overloading, decided at compile time.
Method overriding
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Woof"); }
}
// use:
Animal a = new Dog();
a.sound(); // Woof (child's version)
Dog overrides sound() with the same signature. Even though a is typed as Animal, the actual object is a Dog, so the child's version runs — decided at runtime.
Compile-time vs runtime
- Overloading = compile-time polymorphism. The compiler chooses the method from the argument types.
- Overriding = runtime polymorphism. The JVM chooses based on the actual object at run time.
This is why overloading is also called static, and overriding dynamic, polymorphism.
Side-by-side comparison
| Point | Overloading | Overriding |
|---|---|---|
| Where | Same class | Parent and child |
| Parameters | Must differ | Must be same |
| Method name | Same | Same |
| Resolved | Compile time | Runtime |
| Needs inheritance | No | Yes |
Common mistakes
- Only changing the return type. Overloading needs different parameters, not just a different return type.
- Different signature when overriding. The child method must match exactly, or it becomes an overload.
- Skipping
@Override. It is optional but catches signature mistakes early. - Expecting overriding without inheritance. Overriding needs a parent-child relationship.
Summary
- Overloading: same name, different parameters, in one class (compile time).
- Overriding: same signature, child replaces parent's method (runtime).
- Overloading needs no inheritance; overriding does.
- Overloading is static polymorphism; overriding is dynamic.
- Use
@Overrideto catch mistakes when overriding.
Next, revise inheritance, or reuse methods safely with Java methods.
Frequently Asked Questions
What is the difference between overloading and overriding?
What is method overloading?
add(int, int) and add(double, double). Java picks the right one from the arguments at compile time.What is method overriding?
Is overloading compile-time or runtime?
What is the @Override annotation for?
@Override tells the compiler you intend to override a parent method. If your signature does not actually match a parent method, the compiler flags an error — a helpful safety check against typos.