Saturday, 8 December 2018

Method Overloading in Java | What is Overloading in Java | Constructor Overloading in Java - Online Help

Method overloading in java:

     Method Overloading in Java is nothing but a possibility to create two or more method with the same name. but different parameter list is referred to as method overloading. When a method is called, it matches up with the method name and then the numbers and the type of arguments.
 
The advantage of Method Overloading in Java is that it provides an easy way to handle default parameters value assume that a method has one required parameter and two optional arguments. Three overloaded forms of this method can be defined. These steps are two or three parameters.

Program:
class overload
{
int num(int x)
{
System.out.println(" double X="x);
return(x);
}
int num( int x,int y)
{
System.out.println("int x and y ="+x" "+y);
retrun(x+y);
}
}
class overloaddemo
{
publicstatic void main(String args[])
{
overload od=new overload();
od.num(8);
od.num(8,8);
}
}

output is:
double X=8
int x and y=8 8

Advantages of Method Overloading:

     - When many methods are performing the same operations. It would be easy to call the same method with different types of arguments.

     - We can override many methods and remember them for the same operation.



No comments:

Post a Comment