Overloading vs Overriding in Java
Last Updated :
14 Oct, 2025
Method Overloading and Method Overriding allow methods with the same name but different behavior, supporting polymorphism, the ability of one name to represent multiple forms.
Method Overloading (achieved at compile time)
Method Overloading occurs when we have multiple methods in the same class with the same name but have different numbers of parameters. It allows performing operations with different inputs.
Java
public class OverloadDemo {
// Method 1: two int parameters
static int my_Sum(int a, int b) { return a + b; }
// Method 2: three int parameters
static int my_Sum(int a, int b, int c)
{
return a + b + c;
}
public static void main(String[] args)
{
System.out.println("my_Sum with 2 int parameters: "
+ my_Sum(4, 6));
System.out.println("my_Sum with 3 int parameters: "
+ my_Sum(4, 6, 7));
}
}
Outputmy_Sum with 2 int parameters: 10
my_Sum with 3 int parameters: 17
Explanation: The my_Sum method is overridden in the child class. At runtime, Java determines which version to call based on the actual object. Calling my_Sum(5, 10) on a Calculator object uses the parent class method, while calling it on an AdvancedCalculator object uses the overridden child class method. This demonstrates runtime (dynamic) polymorphism.
Method Overriding (achieved at run time)
Method Overriding is redefining a superclass method in a subclass with the same signature and a compatible return type. It enables runtime polymorphism, where the JVM calls the method based on the actual object type.
Java
// Parent class
class Calculator {
// Method to sum two integers
int my_Sum(int a, int b){
System.out.println("Parent class sum method:");
return a + b;
}
}
// Child class
class AdvancedCalculator extends Calculator{
// Overriding the my_Sum method
@Override int my_Sum(int a, int b){
System.out.println(
"Child class overridden sum method:");
// Adding custom behavior
return a + b
+ 10; // adds extra 10 for demonstration
}
}
// Main class
public class OverrideDemo {
public static void main(String[] args)
{
Calculator calc1 = new Calculator();
System.out.println("Result: "
+ calc1.my_Sum(5, 10));
AdvancedCalculator calc2 = new AdvancedCalculator();
System.out.println("Result: "
+ calc2.my_Sum(5, 10));
// Polymorphism example
Calculator calc3 = new AdvancedCalculator();
System.out.println("Result: "
+ calc3.my_Sum(5, 10));
}
}
OutputDerivedClass display() method
BaseClass show() method
Explanation: Even though bs is a BaseClass reference, it actually points to a DerivedClass object. So, when we call display(), Java runs the DerivedClass version because it’s overridden. The show() method wasn’t changed in the subclass, so calling it just runs the BaseClass version.
Method Overloading vs Method Overriding
The differences between Method Overloading and Method Overriding in Java are as follows:
Feature | Method Overloading | Method Overriding |
---|
Definition | Defining multiple methods with the same name but different parameters in the same class. | Redefining a parent class method in the subclass with the same signature and compatible return type. |
Purpose | To achieve compile-time polymorphism (static binding). | To achieve runtime polymorphism (dynamic binding). |
Parameter List | Must be different (in number, type, or order). | Must be exactly the same as in the parent class. |
Return Type | Can be same or different, but must not conflict. | Must be same or covariant (subtype of parent’s return type). |
Inheritance | Not required; can occur within the same class. | Requires inheritance between superclass and subclass. |
Access Modifier | Can have any access modifier. | Cannot reduce parent method’s access level. |
Static / Final Methods | Can be overloaded. | Cannot be overridden if marked static or final. |
Binding Time | Compile-time binding. | Runtime binding. |
Exception Handling | Overloaded methods can declare any exceptions. | Overridden method cannot throw broader checked exceptions than the parent. |
Example | void add(int a, int b) and void add(double a, double b) | Parent: void show() → Child: void show() |
Difference Between Method Overloading and Method Overriding in Java
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java