Method Overloading definition can be comprised with three points :
1. Same method name
2. Different argument list
3. By changing order of parameters.
Method Overloading is used when objects are required to perform the same task with different input parameter..We can understand method overloading more clearly by below example
Class Rectangle
{
int length,breadth;
//Default Constructor
Rectangle( )
{
length=0;
breadth=0;
}
//Parameterized Constructor
Rectangle(int x,int y )
{
length=x;
breadth=y;
}
int area( )
{
int a;
a=length * breadth;
return(a );
System.out.println("Area of rectangle is:"+a );
}
Class calarea
{
public static void main( string args[])
{
Rectangle rect1= new Rectangle();
Rectangle rect2= new Rectangle(10,15);
rect1.area( );
rect2.area( );
}
}
In this program we can see that same method "Reactangle" is using with two different parameter list so simply we can say that the constructor method is being overloaded...
First the method name will be matched after that it will match the number and type of the parameters and according to this call the function.
Method Overloading can not be achieved with two points :
1) By changing return type
2) By only changing parameter name