July 8, 2022

Method Overloading in Java

In Java you can have two or more methods having the same name with in the same class provided their arguments differ in either type or number. These types of methods are called overloaded methods and the process is known as method overloading in Java.

Method overloading in Java – What qualifies as overloaded method

1- Overloaded methods in Java can have different number of parameters.

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

2- Overloaded methods in Java can also differ by the type of parameters.

add(int a, int b);
add(float a, float b);

3- Even if both the number of parameters and the type of parameters are same but where they appear differs then also methods are considered to be overloaded.

add(int a, float b);
add(float a, int b);

4- In case child class has a method having the same name as in parent class but the parameters differ then also it is method overloading in Java. Note that if methods have the same name and same number/types of parameters in both parent and child class then it is called method overriding in Java.

Method overloading in Java – What doesn’t qualify as method overloading

If methods in the class differ only by return type then the methods are not considered as overloaded methods.

public int add(int a, float b);
public float add(int a, float b);

In these two add methods parameters are same in count as well as type, only return type differs so these are not considered as overloaded methods.

Calling the correct overloaded method

Static or compile time polymorphism in Java is supported through Method overloading. A call to an overloaded method is bound to the correct method at the compile time itself using the type and/or number of arguments.

Method overloading in Java – Example when number of arguments differ

public class OverloadingDemo {

  public static void main(String[] args) {
    OverloadingDemo od = new OverloadingDemo();
    // Calling add method with 2 arguments
    System.out.println("Add- "+ od.add(6, 11));
    // Calling add method with 3 arguments
    System.out.println("Add- "+ od.add(5, 12, 9));
  }

  private int add(int a, int b) {
    return a+b;
  }

  private int add(int a, int b, int c) {
    return a+b+c;
  }
}
Output
Add- 17
Add- 26

Method overloading in Java – Example when types of arguments differ

public class OverloadingDemo {

  public static void main(String[] args) {
    OverloadingDemo od = new OverloadingDemo();
    // Calling add method with 2 arguments
    System.out.println("Add- "+ od.add(6, 11));
    // Calling add method with double arguments
    System.out.println("Add- "+ od.add(5.3, 22));
  }

  private int add(int a, int b) {
    return a+b;
  }

  private double add(double a, double b) {
    return a+b;
  }
}
Output
Add- 17
Add- 27.3

Method overloading in Java – Inheritance example

With inheritance if parent class and child class both have a method having the same name but arguments are different then also the methods are considered to be overloaded.

class A {
  public int add(int a, int b) {
    System.out.println("in add method of the parent class" );
    return a+b;
  }
}

public class B extends A{
  public int add(int a, int b, int c) {
    System.out.println("in add method of the child class" );
    return a+b+c;
  }
    

  public static void main(String[] args) {
    B obj = new B();
    System.out.println("Add- " + obj.add(9, 3));
    System.out.println("Add- " + obj.add(9, 3, 11));
  }
}
Output
in add method of the parent class
Add- 12
in add method of the child class
Add- 23

Here with the object of type B when the add method with two arguments is called the call is to the parent class add method. When the add method with three arguments is called the call is to the child class add method.

Method overloading in Java and automatic type promotion

When an overloaded method is called Java tries to find the method with the exact matching parameters both count and type wise. In case exact match is not found Java tries to resolve the method call by promoting the data types of the method and by looking for a method with higher data type. In automatic type promotion type is promoted to the next highest type and then to next highest type if method call is still not resolved. Order for automatic numeric type promotion is as follows-

byte – short – int – long- float - double

Let’s try to clear it with an example.

public class OverloadingDemo {

  public static void main(String[] args) {
    OverloadingDemo od = new OverloadingDemo();
    // Calling add method with three int arguments
    System.out.println("Add- "+ od.add(5, 22, 12));
  }

  private int add(int a, int b) {
    System.out.println("in add method with int args");
    return a + b;
  }

  private float add(float a, float b, float c) {
    System.out.println("in add method with float args");
    return a + b + c;
  } 
  private double add(double a, double b, double c) {
    System.out.println("in add method with double args");
    return a + b + c;
  }
}
Output
in add method with float args
Add- 39.0

Here we have three overloaded add() methods, two of them have three parameters one having float type parameters and another having parameters of type double. There is a call to add() method with 3 int parameters, since there is no add method having 3 int parameters so the add method having three float parameters is called after automatically promoting int to float.

Since float comes before double in type hierarchy so method having float parameters is called.

That's all for the topic Method Overloading in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment