Method Overloading is a feature in Java where multiple methods have the same name but different parameters.
It helps improve code readability and allows you to use the same method name for different tasks.
Rules for Method Overloading
To overload a method, you must change at least one of the following:
- Number of parameters
- Type of parameters
- Order of parameters
- Changing only the return type is NOT allowed
Example 1: Different Number of Parameters
class Calci {
public static void main(String[] args) {
Calci obj = new Calci();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
int add(int a, int b) {
System.out.println("2 arguments");
return a + b;
}
int add(int a, int b, int c) {
System.out.println("2 arguments");
return a + b + c;
}
}
Output
Example 2: Different Data Types
class Student {
public static void main(String[] args) {
Student obj = new Student();
obj.show(100);
obj.show("Harini");
}
void show(int a) {
System.out.println("Integer: " + a);
}
void show(String a) {
System.out.println("String: " + a);
}
}
Output
Example 3: Order of Parameters
class Test {
public static void main(String[] args) {
Test obj = new Test();
obj.print(10, "Java");
obj.print("Hello", 20);
}
void print(int a, String b) {
System.out.println(a + " " + b);
}
void print(String b, int a) {
System.out.println(b + " " + a);
}
}
Output
Step-by-Step Execution
- Program starts from main() method
- Object is created
- Method is called
- Java checks:
- Number of arguments
- Data type of arguments
- Matching method is executed
- Output is displayed



Top comments (0)