Clean β’ Professional
In Java, Method Overloading and Method Overriding are core OOP concepts used to achieve Polymorphism β the ability of a method to behave differently based on context. Both make programs flexible, readable, and reusable.
Occurs within the same class, when multiple methods have the same name but different parameters (type, number, or order).
Decision made at compile time β Compile-time polymorphism.
Syntax
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; }
}Example β Method Overloading
public class OverloadingExample {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum (int): " + calc.add(10, 20));
System.out.println("Sum (double): " + calc.add(5.5, 4.5));
System.out.println("Sum (3 ints): " + calc.add(1, 2, 3));
}
}
Output:
Sum (int): 30
Sum (double): 10.0
Sum (3 ints): 6class Student {
String name; int age;
Student(String name) { this.name = name; }
Student(String name, int age) { this.name = name; this.age = age; }
void show() { System.out.println(name + " - " + age); }
}
public class ConstructorOverloading {
public static void main(String[] args) {
Student s1 = new Student("Aman");
Student s2 = new Student("Neha", 20);
s1.show(); s2.show();
}
}
Output:
Aman - 0
Neha - 20
Occurs between parent and child classes, when a subclass provides a new implementation of a method already defined in its superclass.
Decision made at runtime β Runtime polymorphism.
Syntax
class Parent { void show() { ... } }
class Child extends Parent { @Override void show() { ... } }Example β Method Overriding
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Dog barks"); }
}
public class OverridingExample {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting
a.sound(); // Calls Dog's version
}
}
Output:
Dog barks
@Override annotation is optional but recommended.class Parent {
void display() { System.out.println("Display from Parent"); }
}
class Child extends Parent {
@Override
void display() {
super.display(); // call parent method
System.out.println("Display from Child");
}
}
public class SuperExample {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}Output:
Display from Parent
Display from Child
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Definition | Same method name, different parameters (same class) | Same method name & parameters (subclass redefines) |
| Polymorphism Type | Compile-time | Runtime |
| Parameters | Must differ | Must be same |
| Return Type | Can differ (if parameter list changes) | Must be same or covariant |
| Access Modifier | Can change | Canβt be more restrictive |
| Static/Final Methods | Can overload | Cannot override |
| Keyword Used | β | @Override |
| Occurs In | Same class | Parentβchild classes |
| Scenario | Use |
|---|---|
| Multiple ways to perform similar operations | Overloading |
| Modify inherited behavior | Overriding |
| Polymorphic behavior with inheritance | Overriding |