Home Java Java - Method Overloading
Java - Method Overloading
Java Method Overloading
When a class has two or more methods by the same name but different parameters, at
the time of calling based on the parameters passed respective method is called (or
respective method body will be bonded with the calling line dynamically). This mechanism
is known as method overloading.
Advantage of Method Overloading
Method overloading improves the code readability and reduces code redundancy. Method
overloading also helps to achieve compile-time polymorphism.
Example of Method Overloading
If you observe the following example, Here we have created a class named Tester this
class has two methods with same name (add) and return type, the only difference is the
parameters they accept (one method accepts two integer variables and other accepts
three integer variables).
class Calculator{
public static int add(int a, int b){
return a + b;
}
public static int add(int a, int b, int c){
return a + b + c;
}
}
When you invoke the add() method based on the parameters you pass respective method
body gets executed.
int result = [Link](1,2); // returns 3;
result = [Link](1,2,3); // returns 6;
Different Ways of Java Method Overloading
Method overloading can be achieved using following ways while having same name
methods in a class.
Use different number of arguments
Use different type of arguments
Invalid Ways of Java Method Overloading
Method overloading cannot be achieved using following ways while having same name
methods in a class. Compiler will complain of duplicate method presence.
Using different return type
Using static and non-static methods
Method Overloading: Different Number of Arguments
You can implement method overloading based on the different number of arguments.
Example: Different Number of Arguments (Static Methods)
In this example, we've created a Calculator class having two static methods with same
name but different arguments to add two and three int values respectively. In main()
method, we're calling these methods and printing the result. Based on the type of
arguments passed, compiler decides the method to be called and result is printed
accordingly.
package [Link];
class Calculator{
public static int add(int a, int b){
return a + b;
}
public static int add(int a, int b, int c){
return a + b + c;
}
}
public class Tester {
public static void main(String args[]){
[Link]([Link](20, 40));
[Link]([Link](40, 50, 60));
}
}
Output
60
150
Example: Different Number of Arguments (Non Static Methods)
In this example, we've created a Calculator class having two non-static methods with
same name but different arguments to add two and three int values respectively. In
main() method, we're calling these methods using object of Calculator class and printing
the result. Based on the number of arguments passed, compiler decides the method to be
called and result is printed accordingly.
package [Link];
class Calculator{
public int add(int a, int b){
return a + b;
}
public int add(int a, int b, int c){
return a + b + c;
}
}
public class Tester {
public static void main(String args[]){
Calculator calculator = new Calculator();
[Link]([Link](20, 40));
[Link]([Link](40, 50, 60));
}
}
Output
60
150
Method Overloading: Different Type of Arguments
You can implement method overloading based on the different type of arguments.
Example: Different Type of Arguments
In this example, we've created a Calculator class having two non-static methods with
same name but different types of arguments to add two int values and two double values
respectively. In main() method, we're calling these methods using object of Calculator
class and printing the result. Based on the type of arguments passed, compiler decides the
method to be called and result is printed accordingly.
package [Link];
class Calculator{
public int add(int a, int b){
return a + b;
}
public double add(double a, double b){
return a + b;
}
}
public class Tester {
public static void main(String args[]){
Calculator calculator = new Calculator();
[Link]([Link](20, 40));
[Link]([Link](20.0, 40.0));
}
}
Output
60
60.0