What is Method overloading? Why is it used in Java?
If multiple functions in a class have the same name but different function definitions it is called method overloading.
It is used to make a java function serve multiple purposes making the code cleaner and API less complex.
Example:
println() prints any data type passed to it as a string.
public class Add_Overload {
void add(int x, int y){
System.out.println(x+y);
}
void add(double x, double y){
System.out.println(x+y);
}
void add(double x, int y){
System.out.println(x+y);
}
public static void main(String args[]){
Add_Overload a= new Add_Overload();
a.add(10,20);
a.add(20.11,11.22);
a.add(20.11,2);
}