Java

Method Overloading In Java

A class may have numerous methods with the same name but different numbers, sequences, or types of arguments thanks to a feature called method overloading. In other words, there are many procedures with the same name but distinct signatures. For instance, the signature of the method add(int a, int b) differs from the signature of the method add(int a, int b, int c) when it has three int arguments.

This is one of Java’s most used OOP features since there are many situations when we require many methods with the same name. For instance, if we were creating a Java application to calculate the sum of the inputted integers, we would want many add method variations according on the inputs from the user, such as add(int, int), add(float, float), etc.

It is comparable to Java’s constructor overloading, which enables a class to have many constructors with various parameter lists.

Three ways to overload a method

The argument lists of the methods must vary in one of these ways in order to overload a method:

1. Number of parameters.

This is a legitimate instance of overloading, for instance.

For instance:

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

2. Data type of parameters.

For example:

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

3. Sequence of Data type of parameters.
For example:

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

Invalid case of method overloading:

The list of arguments does not necessarily indicate the method’s return type; for instance, if two methods have the same name, the same parameters, but have distinct return types, this is not a legitimate example of method overloading. A compilation error will result from this.

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

Overloading of methods is an example of static polymorphism. In a separate lesson, we will go into polymorphism and its many varieties.

Points to Remember:

  1. Compile time binding or early binding are other names for static polymorphism.
  2. Static binding occurs at the time of compilation. Static binding, such as that used in method overloading, binds a method call to its declaration at compile time.

Argument list vs. parameter list: Both terms are used when calling a method, but they have distinct meanings depending on the context. When we define a method, the parameters are referred to as the parameter list.

Method Overloading examples

Method overloading, which was covered at the beginning of this article, is accomplished by defining the same method with several signatures. Here are a few illustrations:

Example 1: Overloading – Different Number of parameters in signature

This example illustrates method overloading via the use of various argument counts. In this example, we have two methods with the same name, add, but with different numbers of arguments. The add() function contains two int arguments in its first iteration, and two int parameters in its second iteration.

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;
  }
}
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

Example 2: Overloading – Data type of parameters are different

In this case, the function add() is being overloaded depending on the arguments’ data types. There are two methods with the same name but distinct argument types. While the second iteration of the add() function uses two float parameters, the first iteration uses two int parameters.

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;
  }
}

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(5.5f, 2.5f));
  }
}

Output:

20
8.0

Example3: Overloading – Sequence of data type of parameters is different

Although the two implementations of the method add() in this example have the same number of arguments, the order of the parameters’ data types varies.

The parameters for the first version are (int, float), whereas those for the second variation are (float, int). Since the sequence is distinct, overloading the method is not problematic.

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;
  }
}
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, 10.5f));

    // This will call the method where first parameter is float
    // and the second parameter is int
    System.out.println(obj.add(1.5f, 1));
  }
}

Output:Method Overloading In Java

Method Overloading and Type Promotion

Type promotion is the process of promoting a smaller data type to a larger data type. Byte data types may be promoted to short, and short data types can be promoted to int, long, double, etc.

What connection does it have to method overloading?
It is crucial to comprehend type promotion because if you don’t, you can believe that a program has a compilation problem while, in reality, it is just fine.

To further understand what I mean, let’s look at an example:

class JavaExample{
  void disp(int a, double b){
    System.out.println("Method A");
  }
  void disp(int a, double b, double c){
    System.out.println("Method B");
  }
  public static void main(String args[]){
    JavaExample obj = new JavaExample();
    /* I am passing float value as a second argument but
     * it got promoted to the type double, because there
     * wasn't any method having arg list as (int, float)
     */
    obj.disp(100, 20.67f);
  }
}

Output:

Method A

As you can see in the output above, I called the disp() function with the second parameter as a float, but it was promoted to the double type since there wasn’t a method with an argument list as float (int, float)

But this kind of advertising doesn’t always occur; here’s another illustration:

class JavaExample{
  void disp(int a, double b){
    System.out.println("Method A");
  }
  void disp(int a, double b, double c){
    System.out.println("Method B");
  }
  void disp(int a, float b){
    System.out.println("Method C");
  }
  public static void main(String args[]){
    JavaExample obj = new JavaExample();
    /* This time promotion won't happen as there is
     * a method with parameter list as (int, float)
     */
    obj.disp(100, 20.67f);
  }
}

Output:

Method C

 

Related Articles

Leave a Reply

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

Back to top button