JAVA- Methods overloading

As like in function overloading in c++ we can overload the methods. In method overloading we can make the methods with the same name but with different parameters. When we call a function then java compiler matches the name and then number of parameters and type of parameters with the function definition.  

e.g:     

class  rectangle

{

int  length;

int  breadth;

getdata()

{

length = 5;

breadth = 8;

}

getdata(int l, int b)

{

length = l;

breadth = b;

}

}

 

Program:

//15.PROGRAM TO IMPLEMENT FUNCTION OVERLOADING

class Rectangle

{

int length,breadth;

Rectangle( )

{

length=10;

breadth=5;

}

Rectangle(int l,int b)

{

length=l;

breadth=b;

}

int area( )

{

return(length*breadth);

}

public static void main(String args[ ])

{

Rectangle r1=new Rectangle( );

Rectangle r2=new Rectangle(20,5);

System.out.println(“Area with length= 10 and breadth= 5 is = “+r1.area( ));

System.out.println(“Area with length =20 and breadth= 5 is  =”+r2.area( ));

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *