Method overloading in Java
Method overloading is just overwriting an existing method with the same method name but varying in parameters part.
Imagine you need to perform addition for two numbers as well as three numbers. The addition method is same but number of values or parameters to be added in both cases are only different. Right?
We should create methods for addition of two numbers and three numbers both separately and the name of the methods should be different. Since both the methods are addition but the names of methods are different, the readability of the program will be reduced. To overcome similar situations, we can define many methods with same name differentiating the methods in parameters part.
This is known as Method overloading.
We can overload a method
- By varying in number of parameters.
- By varying in datatype of the parameters.
By varying in number of parameters
Example
void addition(int a,int b){
int sum=a+b;
System.out.println("Sum of two numbers is "+sum);
}
void addition(int a,int b,int c){
int sum=a+b+c;
System.out.println("Sum of three numbers is "+sum);
}
Full program
class OverloadingTest1{
//addition method of parameter a and b
void addition(int a,int b){
int sum=a+b;
System.out.println("Sum of two numbers is "+sum);
}
//addition method of parameter a, b and c
void addition(int a,int b,int c){
int sum=a+b+c;
System.out.println("Sum of three numbers is "+sum);
}
public static void main(String args[]){
OverloadingTest1 ovl=new OverloadingTest1();
ovl.addition(2,5,10);
ovl.addition(15,10);
}
}
Try In EditorOutput
Sum of three numbers is 17
Sum of two numbers is 25
By varying in datatype of parameters
Example 1
void display(int num){
System.out.println("Displaying the number "+num);
}
void display(char c){
System.out.println("Displaying the character "+c);
}
Full program
class OverloadingTest2{
//display method of parameter num of int datatype
void display(int num){
System.out.println("Displaying the number "+num);
}
//display method of parameter num of char datatype
void display(char c){
System.out.println("Displaying the character "+c);
}
public static void main(String args[]){
OverloadingTest2 ovl=new OverloadingTest2();
ovl.display(1);
ovl.display('d');
}
}
Try In EditorOutput
Displaying the number 1
Displaying the character d
Example 2
void addition(int a,int b){
int sum=a+b;
System.out.println("Sum of two int values is "+sum);
}
void addition(int a,float b){
float sum=a+b;
System.out.println("Sum of int and float value is "+sum);
}
Full program
class OverloadingTest3{
//addition method of two int parameters a and b
void addition(int a,int b){
int sum=a+b;
System.out.println("Sum of two int values is "+sum);
}
//addition method of int parameter a and float b
void addition(int a,float b){
float sum=a+b;
System.out.println("Sum of a int and float value is "+sum);
}
public static void main(String args[]){
OverloadingTest3 ovl=new OverloadingTest3();
ovl.addition(55,100);
ovl.addition(55,150.97f);
}
}
Try In EditorOutput
Sum of two int values is 155
Sum of a int and float value is 205.97