
How can we use method overloading in java program?

Method overloading:?In method overloading methods have same name but different type of parameters. Here is an example of method overloading-
Example:
public class MethodOverloading{
public void add(int a, int b){
System.out.println(a + b);
}
public void add(String x,String y){
System.out.println(x + y);
}
public void add( char c, char d){
System.out.println(c + d);
}
public static void main(String [] args){
Calculation c = new Calculation();
c.add(7,8);
c.add("he", "llo");
c.add('n','k');
}
}
Output
15 hello 217
Description:- We have created a class named MethodOverloading. Method add () is overloaded in our example. This method is adding the passed parameters. In add(int a, int b),we are passing two int type parameter. and in the same method add(String x,String y) ,we are passing two String type parameter. And lastly in add( char c, char d) we are passing two char type parameters. In main method we are calling the all three overloaded add() methods.

public class MethodOverloading{
public void add(int a, int b){
System.out.println(a + b);
}
public void add(String x,String y){
System.out.println(x + y);
}
public void add( char c, char d){
System.out.println(c + d);
}
public static void main(String [] args){
MethodOverloadingc = new MethodOverloading();
c.add(7,8);
c.add("he", "llo");
c.add('n','k');
}
}