Difference Between Method Overriding and Method Overloading in Java
Feature Method Overriding Method Overloading
Definition Redefining a parent class Defining multiple methods
method in a subclass with the same name but
different parameters
Class Requirement Requires inheritance (two Occurs within the same
classes involved) class
Parameters Must be exactly same as the Must be different (type,
parent method number, or order)
Return Type Same or covariant (subclass Can be same or different
of return type)
Polymorphism Runtime Polymorphism Compile-time
(dynamic binding) Polymorphism (static
binding)
Access Modifier Cannot be more restrictive No such rule
than parent method
Purpose To provide specific To perform different tasks
behavior in the subclass with the same method name
✅ Overriding Example
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
✅ Overloading Example
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;
}
}
🔍 Quick Summary:
- Overriding: Same name + same parameters, different classes
- Overloading: Same name + different parameters, same class