Method Overloading
Creating a multiple methods with same name and with different argument list is known as method overloading.
When we are overloading a method argument, list should be unique i.e, it should be unique with either number of arguments or type of an argument.
We use overloading when we have a same functionality for different data types.
When a overloaded method is called, a particular method will be invoked based on data's passed.
Example of Method Overloading:
Class Run
{
public static void main(String[] args)
{
System.out.println("Program Starts");
System.out.println(add(13.456,12.123));
System.out.println(add(10,30));
System.out.println(add(12,15.345));
System.out.println("Program end");
}
static int add(int n1, int n2)
{
return n1+n2;
}
static double add(int n1, double n2)
{
return n1+n2;
}
static double add(double n1, double n2)
{
return n1+n2;
}
}
0 Comment(s)