Method overloading in java

Method Overloading in java

In this tutorial, we will learn about method overloading in Java. Method Overloading is a feature that allows a class to have multiple methods with the same name but with different number, sequence or type of parameters.

In short multiple methods with same name but with different signatures. For example the signature of method add(int a, int b) having two int parameters is different from signature of method add(int a, int b, int c) having three int parameters.

This is one of the most popular OOP feature in java, there are several cases where we need more than one methods with same name.

Let’s see the overload methods in Java.

Method overloading is a feature in Java that allows a class to have more than one method which has the same name, even if their arguments vary. It is also done within the same class with different parameters. Overloading is sometimes also referred to as compile-time polymorphism. Overriding is a similar concept in java. It varies with method overloading as it is performed in different classes having the same parameters. One class is the parent class and the other is the child class. Overriding methods have the inheritance relationship within the classes.

Read about Polymorphism in JAVA

There are different ways of performing overloading:

(1) Overloading by changing the number of arguments.

For example :

add(int, int)

add(int, int, int)

This below example shows how method overloading is done by having different number of parameters. In this example, we have two methods with the same name add, but number of parameters are different. First variation of add() method has two int parameters, while the second variation of method add() has two int parameters.

Code :

class DisplayOverloading

{

  //adding two integer numbers

  int add(int a, int b)

  {

    int sum = a+b;

    return sum;

  }

  //adding three integer numbers

  int add(int a, int b, int c)

  {

    int sum = a+b+c;

    return sum;

  }

}

public class JavaExample

{

  public static void main(String args[])

  {

    DisplayOverloading obj = new DisplayOverloading();

    System.out.println(obj.add(10, 20));

    System.out.println(obj.add(10, 20, 30));

  }

}


Output :

30

60

(2) By changing the data types of the parameters.

For example :

add(int, int)

add(int, float)

In this below example, we are overloading the method add() based on the data type of parameters. We have two methods with the name add() but the type of parameters are different. The first variation of add() has two int params while the second variation of method add() has two float params.

Code :

class DisplayOverloading2

{

  //two int parameters

  public int add(int a, int b)

  {

    int sum = a+b;

    return sum;

  }

  //two float parameters

  public float add(float a, float b)

  {

    float sum = a+b;

    return sum;

  }

}

public class JavaExample

{

  public static void main(String args[])

  {

    DisplayOverloading2 obj = new DisplayOverloading2();

    //This will call the method add with two int params

    System.out.println(obj.add(5, 15));

    //This will call the method add with two float params

    System.out.println(obj.add(95, 5));

  }

}


Output :

20 100

(3) By changing the order of the parameters

For example :

add(int, float)

add(float, int)

In this example, the both the variations of method add() has same number of parameters but the sequence of data type of parameters is different.

First variation has (int, float) parameters and the second variation has (float, int) parameters. Since the sequence is different, the method can be overloaded without any issues.

class DisplayOverloading3

{

  public float add(int a, float b)

  {

    System.out.println("Method with (int, float) param list.");

    return a+b;

  }

  public float add(float a, int b)

  {

    System.out.println("Method with (float, int) param list.");

    return a+b;

  }

}

public class JavaExample

{

  public static void main(String args[])

  {

    DisplayOverloading3 obj = new DisplayOverloading3();

    // This will call the method where first parameter is int

    // and the second parameter is float

    System.out.println(obj.add(10, 90.5f));

    // This will call the method where first parameter is float

    // and the second parameter is int

    System.out.println(obj.add(45.5f, 1));

  }
}


Output :

100.5

46.5

The main advantages of overloading are:

  • More than two methods can have the same name inside that same class with different arguments.
  • We cannot perform method overloading by changing the return type of the arguments.
  • It maintains the cleanliness of the code.
  • It increases the quality of reading of a program, along with its flexibility.
  • The method can also be used as a constructor for creating new objects.
  • It can take a huge amount of data.
  • Ambiguities can be avoided by using different numbers and types of arguments.

Method overloading Examples

public class Subtraction { 
    public int subtraction(int a, int b){ 
        return (a - b); 
    } 
    public int subtraction(int a, int b, int c){ 
        return (a - b - c); 
    } 
    public double subtraction(double a, double b){ 
        return (a - b); 
    } 
    public static void main(String args[]) { 
        Subtraction s = new Subtraction(); 
        System.out.println(s.subtraction(300, 200)); 
        System.out.println(s.subtraction(300, 200, 100)); 
        System.out.println(s.subtraction(300.5, 200.5)); 
    } 
}


Output :

100
0
100

From the above example, we can see that we don’t have to remember different names for functions that are performing the same function in the program. For example, in the code, if overloading was not performed by Java, we would have to create method names like subtraction1, subtraction2, … or subtraction2Int, subtraction3Int, … etc.

However, we cannot perform method overloading by changing the return type of the method. It is because of the rise of ambiguity in the code. It can generate a compile-time error while compilation. Below is a program for elaboration:

class Subtractor {
    static int subtract(int x, int y) {
        return x - y;
    }

    static double subtract(double x, double y) {
        return x - y;
    }
}
public class Overloading {
    public static void main(String[] args) {
        System.out.println(Subtractor.subtract(200, 100)); // Output: 100
        System.out.println(Subtractor.subtract(200.5, 100.2)); // Output: 100.3
    }
}


The output will be:

100
100.3

The frequent question that arises while executing the process of method overloading is can we overload the main function in java?

The answer is yes. We can overload the main() in java by method overloading. We can have an unlimited number of main() in a single class by method overloading. The main() can be called inside public static void main(String args[]) in JVM.

It is also possible to overload static methods in java by having multiple static methods with the same name and different input parameters. However, we cannot overload two methods if they have the different static keyword in the code.

Difference between method Overloading and Method Overriding

As we have discussed in the previous section, method overloading is always performed in the same classes. However, the process of method overriding is performed between the parent and the child class. Overriding is the process where the child class inherits the methods of the parent class using the IS-A relationship.

Method overloading is an example of compile-time polymorphism whereas method overriding is an example of run time polymorphism. There are no hard rules for return type in method overloading. The return type can either be the same or different in this case. However, in the case of method overriding, the return type has to be the same.

It is also possible to overload static methods in java by having multiple static methods with the same name and different input parameters. However, we cannot overload two methods if they have the different static keyword in the code.

One comment on “Method Overloading in java

  • KsiΔ…ΕΌki Psychologia , Direct link to comment

    Thanks for the nice writing. I have bookmarked your site and will be back to read more!

Leave a comment

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

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!