Method Overloading in Java (with Examples)

When a class has more than one method having the same name but with different parameter lists, this feature is called method overloading in Java.

In other words, when you declare multiple methods with the same name but different method signatures in a class, these methods are overloaded methods in Java. This concept is referred to as method overloading.

Method overloading is a powerful and important feature of object-oriented programming (OOPs) in Java. It is one of the ways that Java implements compile-time polymorphism, also known as static polymorphism.

Method overloading allows you to write cleaner and more organized code by grouping logically similar actions under the same method name. This improves the maintainability and readability of the code. Let’s make the definition of method overloading in Java to understand easy with the help of a real-time example.

Real-Time Example of Method Overloading in Java


Imagine you have a vehicle. When you apply the brake, the vehicle stops. The brake() method represents the behavior of stopping the vehicle. In a car, the brake is applied with the leg, while in a bicycle, it is applied with the hand. Though the way of applying brakes is different, but the desired behavior to stop the vehicle is the same.

This analogy is similar to method overloading in Java. You can define multiple brake() methods with the same name but different parameters, depending on the type of vehicle, within a class. If the behavior of more than one method with the same name performs the same thing or action using different numbers or types of parameters, it is called method overloading in Java.

How does Java Differentiate and Determine Which Overloaded Method to Call?


When you define multiple methods with the same name but different parameter lists in a class, Java compiler differentiates overloaded methods based on their method signatures.

A method signature is defined by the method name and the list of parameters (number, type, and order of parameters). The return type of a method is not part of the method signature. It does not play any role in resolving overloaded methods.

At compile-time, the compiler selects the appropriate overloaded method to execute based on the reference type and the arguments passed in the method call. It matches the method call with the method signature that has the most compatible parameter list. Thus, method overloading is resolved by Java compiler during compile-time, which is why it is also known as compile-time polymorphism or static polymorphism.

Although objects are created at runtime by the JVM, the JVM does not decide which overloaded method to call. In other words, JVM does not play a role in selecting the overloaded method. It simply executes the bytecode produced by the compiler.

You should always use method overloading technique with caution as Java compiler does not consider the return type while differentiating between overloaded methods.

Rules for Implementing Method Overloading in Java


Here is the list of rules to follow when you will implement method overloading in Java:

  • The method name must be the same. In other words, all overloaded methods must share the identical name.
  • Parameters must be different. Each overloaded method must have a unique list of parameters. You can achieve this using any of three ways:
    • Data type of parameters
    • Number of parameters
    • Sequence (i.e. order) of data type of parameters
  • The return type of the method does not affect overloading. If two methods have the same name and different return types, they cannot be overloaded if their parameter list is identical.
  • Access modifiers do not affect method overloading. You can overload methods even if they have different access modifiers.
  • Exception thrown can be anything. A method can throw any exception (checked or unchecked). It doesn’t affect overloading.
  • The implementation does not matter in the case of method overloading. A method can have any kind of logic.

Valid Overloading Examples:

Let’s see some valid examples of method overloading in Java.

1. Different Data types of Parameters

void add(int a, int b) { } // (int, int)
void add(int a, float b) { } // (int, float)

The data type is distinct in both cases because their parameter types differ (int, int and int, float). So, this is a valid case of method overloading. If we call the method add(10, 20) by passing argument values, JVM invokes the first method. If we invoke add(10, 20.5f), JVM executes the second method.

2. Different Number of Parameters

void sum(int a, int b) { } // Two parameters a and b of data type int.
void sum(int a, int b, int c) { } // Three parameters a, b, and c of data type int.

Here, the number of parameters is different, but the data type is the same. This is also a valid case because there is a difference in the number of parameters passed to methods.

3. Different Sequence of Data type of Parameters

void sub(int a, double b) { }
void sub(double a, float a) { }

Here, the sequence of data type of parameters is different. So, this is a valid case of method overloading in Java. In the above three valid examples of method overloading, the compiler can easily distinguish them.

Key Features of Method Overloading in Java


There are several important features of method overloading in Java that you should have to keep in mind.

1. The call to overloaded method is bonded at compile-time. The compiler decides which overloaded method to call based on the reference type at compile time, not runtime object type.

2. The concept of method overloading is also known as compile-time polymorphism in Java because the method binding happens during compilation.

3. Method overloading is generally done in the same class. But you can also do it in the subclass. You need to make a relationship between the parent class and child class by using extends keyword for it.

4. Method overloading cannot be done by changing only the return type of the method because there may occur ambiguity and Java will throw a compile-time error. However, overloaded methods can have different return types if their parameter list is also different.

5. You can overload the private methods in Java.

6. The final methods can be overloaded in Java.

7. The main method can also be overloaded in Java.

8. You can overload both static and instance methods in Java. Method overloading is possible when two or more static methods with the same name, but the difference in the list of parameters.

When to Use Method Overloading in Java


Method overloading is a powerful feature in Java but you should use as per needs. You should use method overloading when:

  • You need to define multiple methods with the same name but different parameters, and they perform the same or similar thing or operation. For example, you want to calculate the area of different shapes (e.g., circle, rectangle, triangle) using a method called area(), but with different parameter types or counts.
  • If multiple methods perform different tasks, don’t use the method overloading concept.

Why Method Overloading is Used in Java?


Method overloading is used in Java programming because of the following needs:

  • Method overloading is done to reuse the same method name.
  • It is done to make the program logically more readable and understandable.
  • It improves code readability and reduces redundancy.
  • Method overloading is used to achieve the compile-time polymorphism in Java.

Method Overloading Example Program


Let’s take some important example programs based on the different ways of method overloading.

1. Method Overloading by Changing the Number of Arguments

Example 1: Let’s write a Java program in which we will do method overloading by defining two sum() methods with different number of parameters. The first method sum() will perform addition of two numbers, while the second method sum() will perform addition of three numbers by overloading concept. Look at program code to understand better.

package methodOverloading; 
public class Addition 
{ 
// Method to calculate the sum of two numbers. 
// Declare an instance method named sum with two parameters a and b of data type int. 
   void sum(int a, int b) 
   { 
      int s = a + b; 
      System.out.println("Sum of two numbers: " +s); 
   } 
// Method to calculate sum of three numbers. 
// Declare an instance method named sum with three parameters a, b, and c of data type int. 
// Here, the method sum() is overloaded with the same method name and different parameter lists. 
   void sum(int a, int b, int c) 
   { 
      int t = a + b + c; 
      System.out.println("Sum of three numbers: " +t); 
   } 
public static void main(String[] args) 
{ 
    Addition a = new Addition(); 
    a.sum(10, 20); // This will call sum() method to calculate sum of two arguments. 
    a.sum(50, 100, 200); // This will call sum() method to calculate sum of three arguments. 
  } 
}
Output: 
       Sum of two numbers: 30 
       Sum of three numbers: 350

As you can see in the above example program, the method sum() is overloaded two times based on the number of parameters. We have declared two sum() methods with the identical name but the different number of parameters. The overloaded methods have calculated the sum of two numbers and three numbers based on the passing of arguments.

An example program showing method overloading in Java.

When a method is called, the Java compiler determines which overloaded method to execute based on the number of arguments passed in the method call. The compiler matches the method call with the method signature that has the most compatible parameter list.

2. Method Overloading by Changing Data Type of Parameters

Example 2: Let’s write a Java program in which we will create two methods sub() with the same name but differ in the data type of parameters. The first method sub() will receive two integer arguments and the second method sub() will receive two double arguments. Look at the program code.

package methodoverload; 
public class Subtraction 
{ 
// Declare a method named sub with two parameters x and y of data type int. 
   int sub( int x, int y) 
   { 
      int a = x - y; 
      System.out.println("Subtraction of two numbers: " +a); 
      return a; 
   } 
// Declare a method named sub with two parameters x and y of data type double. 
   double sub(double x, double y) 
   { 
     double b = x - y; 
     System.out.println("Subtraction of two numbers: " +b); 
     return b; 
   } 
public static void main(String[] args) 
{ 
    Subtraction s = new Subtraction(); 
    s.sub(6, 5); // This will call sub() method to calculate the subtraction of two int type arguments. 
    s.sub(20.8, 10.9); This will call sub() method to calculate the subtraction of two double type arguments. 
  } 
}
Output: 
       Subtraction of two numbers: 1 
       Subtraction of two numbers: 9.9

In this example, the method sub() is overloaded two times based on data types of parameters. The overloaded methods have calculated subtraction of two numbers based on data type of parameters.

3. Method Overloading by Changing Sequence of Data Type of Parameters

Example 3: Let’s write a Java program where we will overload methods by changing the sequence of data type of parameters.

package methodOverloading; 
public class Multiplication 
{ 
  void multiply(int a, double b) 
  { 
     double m1 = a * b; 
     System.out.println("Multiplication of two numbers: " +m1); 
  } 
  void multiply(double a, int b) 
  { 
     double m2 = a * b; 
     System.out.println("Multiplication of two numbers: " +m2); 
  } 
public static void main(String[] args) 
{ 
   Multiplication m = new Multiplication(); 
   m.multiply(10, 20.5); // Calls multiply() to calculate the multiplication of two arguments int and double. 
   m.multiply(10.5, 30); // Calls multiply() to calculate the multiplication of two arguments double and int. 
  } 
}
Output: 
       Multiplication of two numbers: 205.0 
       Multiplication of two numbers: 315.0

In the above example, the method multiply() is overloaded two times based on the sequence of parameter types. The overloaded methods have calculated multiplication of two numbers based on the sequence of argument types while calling methods at compile time by the compiler.

Method Overloading Done in Subclass


Example 4: Let’s write a Java program in which we will perform method overloading in the child class (i.e. subclass). We will make a relationship between the parent class and child class by using extends keyword.

We will create one method msg() in the parent class and second method msg() in the subclass. While calling from the class ‘Test’, the msg() will receive int argument and double argument.

package methodOverloading; 
public class SuperClass 
{ 
  void msg(int x, int y) 
  { 
    System.out.println("Hello Java"); 
  } 
} 
public class Subclass extends SuperClass 
{ 
  void msg(double x, double y) 
  { 
    System.out.println("Welcome you in Java programming"); 
  } 
} 
public class Test { 
public static void main(String[] args) 
{ 
     Subclass sc = new Subclass(); 
     sc.msg(10, 20); 
     sc.msg(2.5, 3); 
   } 
}
Output: 
       Hello Java 
       Welcome you in Java programming

Why Method Overloading is Not Allowed by Return Type Only in Java?


In Java, method overloading cannot be done when the return type, method name, and argument list are the same because there may occur ambiguity during method calling. Let’s take an example to demonstrate how ambiguity may occur when trying to perform method overloading by changing only the return type.

Example 5:

package methodOverloading; 
public class ReturnTypeEx1 
{ 
  public int m1(int a, int b) // Duplicate method error. 
  { 
     int x = a + b; 
     return x; 
  } 
  public int m1(int c, int d) // Duplicate method error. 
  { 
     int y = c * d; 
     return y; 
  } 
 } 
public class ReturntypeTest { 
public static void main(String[] args) 
{ 
    ReturnTypeEx1 obj = new ReturnTypeEx1(); 
    int sum = obj.m1(20, 30); 
    System.out.println(sum); 
    int multiply = obj.m1(20,30); 
    System.out.println(multiply); 
  } 
}
Output: 
        50

As you can see in the above example program, we cannot do method overloading because the compiler shows the duplicate method error.


Example 6: Let’s take another program where we use the same method name and argument list, but change only the return type to see why it causes a compile-time error.

package methodOverloading; 
public class ReturnTypeEx1 
{ 
  public int m1(int a, int b) // Return type is int. 
  { 
     int x = a + b; 
     return x; 
  } 
 public double m1(int c, int d) // Return type is double. 
 { 
     int y = c * d; 
     return y; 
  } 
} 
public class ReturntypeTest { 
public static void main(String[] args) 
{ 
    ReturnTypeEx1 obj = new ReturnTypeEx1(); 
    int sum = obj.m1(20, 30); 
    System.out.println(sum); 

    int multiply = obj.m1(20,30); 
    System.out.println(multiply); 
   } 
}
Output: 
        50

The compiler again shows the duplicate method error because the compiler checks only method signature for duplication, not the return type. Therefore, method overloading is not possible even though their return type is different. So, the return type does not play any role in the case of Java method overloading.

Can We Overload Main Method in Java?


Yes, we can overload the main() method in Java. A class can have many main() methods with different parameter lists. However, the JVM calls that main() method that receives string array as an argument only. Therefore, the execution always starts from public static void main(String[] args) only. This is the entry point of the program.

To understand this concept more clearly, let’s take an example program that demonstrates main method overloading.

Example 7: 

public class MainMethodOverloadingTest 
{ 
   public static void main(String[] args) 
   { 
      System.out.println("main(String[] args)"); 
      main(); 
   } 
   public static void main() 
   { 
      System.out.println("main without args"); 
   } 
public static void main(String args) 
{ 
    System.out.println("main with string args"); 
 } 
}
Output: 
       main(String[] args) main without args

Valid/Invalid Cases of Method Overloading


Let’s see a few valid or invalid cases of the method overloading in Java.

Case 1:

void sum(int a, int b, float c) { }
void sum(int c, int d, float e) { }

Result: Compile-time error. Here, the argument lists are exactly the same. Both methods are having the same number of parameters, and the same sequence of data types of the parameters.

Case 2:

int sum(int a, int b) { }
int sum(double c, double d) { }

Result: Perfectly fine. This is a valid case of method overloading because data types of arguments are different.

Case 3:

void msg(String a, char c) { }
void msg(char x) { }

Result: Perfectly fine. A valid case of method overloading. Here, the number of parameters is different.

Case 4:

void msg() { }
void msg(char x) { }

Result: This is a valid case of method overloading because the number of parameters is different. There is no parameter in the first method and the second method has one parameter.

Case 5:

void msg(Object o, char a) { }
void msg(char x, Object o) { }

Result: This is a valid case of method overloading. This is because the sequence of data types of parameters is different. The first method is having (Object, char) and the second method has (char, Object).

Case 6:

Object msg(String s) { }
String msg(String a) { }

Result: Duplicate method error. Argument lists are the same. Even though return type of methods is different. Still, it is an invalid case of method overloading. The return type of method does not play any role while overloading a method.

Q. Identify the error in the following code.

public class Test {
    public static void m1(int a) {
	
    }
    public static int m1(int y){
       return y;
    }
}

Answer: Duplicate method error by the compiler.

Advantages of Method Overloading


The advantages of using method overloading concept in Java are as follows:

  1. In Java, method overloading provides the same method name to reuse in the program.
  2. Method overloading helps to increases the readability and understanding of the program.
  3. It makes the program logically more readable and understandable.
  4. Method overloading helps in achieving the compile-time polymorphism in Java.
  5. It is useful to avoid repeating same code in different methods.

Disadvantages of Method Overloading


Following are the disadvantages of using method overloading in Java:

  • Too many overloaded methods in a class can make the code harder to read, understand, and maintain, especially for beginners.
  • Maintenance or debugging can be more complex and time-consuming if too many overloaded methods exist in a class.
  • If the method overloading is not used carefully, it can lead to ambiguous method calls, especially with type conversions.
  • You cannot overload a method just by changing the return type. This restriction can limit the certain scenarios and may require renaming methods unnecessarily.