Java is an object-oriented programming language that supports polymorphism, allowing developers to write more flexible and reusable code. Two key features of polymorphism in Java are Method Overloading and Method Overriding.
While both allow methods to behave differently in different situations, they are quite different in their purpose, rules, and execution.
In this article, we will explore the differences between Method Overloading and Method Overriding in Java, understand their use cases, and review real-world code examples to clarify the concepts.
📘 What is Method Overloading?
Method Overloading occurs when a class has multiple methods with the same name but different parameters (arguments). It’s a way to achieve compile-time polymorphism or static polymorphism.
✅ Key Rules of Method Overloading:
- Methods must have the same name.
- Methods must differ in number, type, or order of parameters.
- Can have same or different return types.
- Can have same or different access modifiers.
- Can be static or non-static.
- Overloading happens within the same class.
Example of Method Overloading:
public class Calculator {
// Method 1
public int add(int a, int b) {
return a + b;
}
// Method 2 - Overloaded with different number of arguments
public int add(int a, int b, int c) {
return a + b + c;
}
// Method 3 - Overloaded with different type of arguments
public double add(double a, double b) {
return a + b;
}
}
✅ Output:
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3)); // 5
System.out.println(calc.add(1, 2, 3)); // 6
System.out.println(calc.add(2.5, 3.5)); // 6.0
📘 What is Method Overriding?
Method Overriding happens when a subclass redefines a method inherited from its superclass with the same method signature. It enables runtime polymorphism or dynamic binding.
✅ Key Rules of Method Overriding:
- Method name, number of parameters, type of parameters, and order must be exactly the same as in the superclass.
- Return type must be same or subtype (covariant).
- Cannot override static, private, or final methods.
- Can increase visibility, but not reduce it.
- Requires inheritance — one class must be a subclass of another.
Example of Method Overriding:
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
✅ Output:
Animal animal = new Dog();
animal.sound(); // Output: Dog barks
Here, the method call is resolved at runtime, and the overridden method in Dog
class is executed.
📊 Method Overloading vs Method Overriding — Comparison Table

🔐 Access Modifiers: Visibility in Overloading vs Overriding
In Overloading:
You can change access modifiers freely:
public void print(int a) { ... }
private void print(double a) { ... } // Valid overload
In Overriding:
You cannot reduce the access modifier:
public class A {
public void show() {}
}
public class B extends A {
protected void show() {} // ❌ Compile-time error
}
✅ You can increase visibility, but not reduce it.
🔒 Static, Final, and Private Methods
❌ Method Overriding Restrictions:
- Static methods: belong to the class, not instance — cannot be overridden.
- Final methods: cannot be changed — not overridable.
- Private methods: not visible to subclass — cannot be overridden.
✅ But they can be overloaded:
class Demo {
private void display() {
System.out.println("Private method");
}
private void display(String msg) {
System.out.println(msg);
}
}
🛠️ Use Cases
Use Method Overloading when:
- You want same functionality with different input types.
- You need to handle different parameter counts or types.
Use Method Overriding when:
- You want subclasses to define their own behavior for an inherited method.
- You need to support runtime polymorphism (e.g., factory pattern, strategy pattern).
❌ Common Mistakes to Avoid
1. Thinking method overloading depends on return type
public int sum(int a, int b) { ... }
// public double sum(int a, int b) { ... } // ❌ Compile-time error (duplicate method)
2. Trying to override a static method
class A {
static void display() {}
}
class B extends A {
static void display() {} // Hides, not overrides
}
Summary

Final Thoughts
Understanding the difference between method overloading and method overriding is essential for mastering polymorphism in Java.
- Use overloading to provide multiple ways to perform similar operations within a class.
- Use overriding to change inherited behavior to match the needs of a specific subclass.
By following the rules and understanding the design intent, you can write cleaner, more maintainable, and polymorphic Java code.
Comments
Post a Comment
Leave Comment