Let’s take an example:

Suppose we have to write a method to find the square of an integer number. We can write this method as follows:

```public void IntSquare( int number )
{
int square = number * number;
System.out.printIn("Method with Integer Argument Called:"+square);
}
```

It will provide output in integer data type.

Now, We want to calculate the square of a double type value,

```public void doubleSquare(double number)
{
double square = number * number;
System.out.printIn("Method with double Argument Called:"+square);
}
```

Similarly, if we want to find the square of short type value, then we have to create another method as follows:

```public void shortSquare(long number)
{
short square = number * number;
System.out.printIn("Method with short Argument Called:"+square);
}
```

If we notice something here, we can clearly see that according to the data type we have taken different names :

1. IntSquare()
2. doubleSquare()
3. shortSquare()

We can do this by using the same name also, the program itself decides which method to use for which type of value, then it will be easier for the programmer to get the same.  There is no need to memorize the names of more than one method for type work. In Java, we can give the above three methods the same name.

Method Overloading is the process to create multiple methods having the same name but different arguments within the same class,

There are two ways to overload the method in java

• A number of parameters.
```add(int, int)
```
• Data type of parameters.
```add(int, int)
```

```int add(int, int)
```

This will throw a compilation error.

Code:

## Java Code

``````// same class
// same method name
// different arguments
public class Student {
}
public void add(int i, int j) {
System.out.println("Sum "+ (i+j));
}
System.out.println("i = "+i);
}

public static void main(String[] args) {
Student obj1 = new Student();  // Create a Shapes object

}
}
``````

Output:

i = 2
Sum 8

Code:

## Java Code

``````// MethodOverloading by changing data type

// this method accepts int
private static void show(int a){
System.out.println("Got Integer.");
}

// this method  accepts float object
private static void show(float a){
System.out.println("Got float.");
}
// this method  accepts float object
private static void show(String a){
System.out.println("Got string.");
}

public static void main(String[] args) {
show(1);
show(1);
show("TUF");
}
}
``````

Output:

Got Integer.
Got Integer.
Got string.

### What if we put the same method name with the same arguments for each method?

Let’s see an example:

Code:

## Java Code

``````public class Student {

System.out.println("Sum "+ (i+j));
}
System.out.println("i = "+i);
}
public static void main(String[] args) {
Student obj1 = new Student();  // Create a Shapes object

//The above line will produce a compile Time Error ....

}
}
``````

The above program will generate a compile-time error. Here, Java cannot determine which add() method to call and hence creates an error. And this condition is called ambiguity.

• Method overloading cannot be done by changing the return type of methods.

Example:

Code:

## Java Code

``````class TUF{
int display(int x){
return x;
}
double display(int y){
return y;
}
public static void main(String args[])
{
TUF s = new TUF();
System.out.printIn("Value of x : " + s.display(5));
System.out.printIn("Value of y : " + s.display(6.5));
}
}
``````

Output: Sample.java:6: error: method display(int) is already defined in class TUF

If there are two methods of the same signature within a class in the program, then an Ambiguity Error comes, whether their return type is different or not. This means that method overloading has no relation to return-type.

• The most important rule of method overloading is that two overloaded methods must have different parameters.