Method overloading in Java

What is Method Overloading in Java?

Before knowing about method overloading, let’s know about the need for method overloading :

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.

And here Method Overloading Comes into the picture.

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

Method overloading is an example of Static Polymorphism.

There are two ways to overload the method in java

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

Invalid case of method overloading:

int add(int, int)
float add(int, int)

This will throw a compilation error.

Example 1: Overloading by changing the number of parameters

Code:

Java Code

// same class
// same method name 
// different arguments
public class Student {
  public void add() {
        System.out.println("Method Overloading ");
    }
    public void add(int i, int j) {
        System.out.println("Sum "+ (i+j));
    }
    public void add(int i) {
       System.out.println("i = "+i);
    }
 
  public static void main(String[] args) {
    Student obj1 = new Student();  // Create a Shapes object
     
    obj1.add();
    obj1.add(2);
    obj1.add(6,2);
     
  }
}

Output:

Method Overloading
i = 2
Sum 8

Example 2: Overloading by changing the data type of parameters

Code:

Java Code

// MethodOverloading by changing data type
 
class MethodOverloading {
 
    // 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 {
 
    public void add(int x) {
        System.out.println("Sum "+ (i+j));
    }
    public void add(int y) {
       System.out.println("i = "+i);
    }
  public static void main(String[] args) {
    Student obj1 = new Student();  // Create a Shapes object
     
   
    obj1.add(2);
    obj1.add(6,2);
     //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.

Some points to remember about method overloading: –

  • 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.

Advantages of Method Overloading:

  • It increases the readability of the program.
  • It allows us to use the same method name for related functions with slight variations in the argument.
  • This makes the code look clean.
  • This reduces the execution time because the binding is done in compilation time itself.

In Java, we can also overload constructor that you can check the article here.

In this way, we can define multiple methods of the same name in a class called Method Overloading. The Java Compiler itself, based on the Data Type of the Arguments of the Methods,  performs the appropriate method call for an object.

Special thanks to Amisha Kumari for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article