Method Overloading in Java with example

In Java, it is possible to create methods that have the same name, but different parameter lists and different definitions that are called Method Overloading. It is used when objects are required to perform similar tasks but using different input parameters.

Example :

class House
{
 int length, width;
House(int x, int y)     // creating constructor 1
{
 length=x;
 width=y;
}
House(int x)           // creating constructor 2
{
length=width=x;
}
int area()
{
return(length*width);
}
public static void main(String arg[])
{
 House h1=new House(); 
 System.out.println(h1.House(20,16));
 House h2=new House();
 System.out.println(h2.House(20));
}
}

Output :
320
20

Recommended Posts:

Java
    1. Features of Java
    2. Java Program Structure
    3. Command Line Arguments in Java
    4. Difference between java and c++
    5. Java Data Types
    6. Scope of Variables in Java
    7. Operators in Java
    8. Looping Statement in Java
    9. Objects and Classes in Java
    10. Constructors in Java
    11. Method Overloading in Java with Program