Java

Method Overloading in Java | Explained

Usually, in any programming language, we create different methods with different names however Java provides a concept of method overloading that enables us to create multiple user-defined Java methods with the same name but with different implementations. To do so there are some rules such as methods definition should be different, methods parameters should be different, and so on. The method overloading approach provides many advantages such as flexibility i.e. calling the same method with different data types, better readability, reduce execution time, etc.

For the profound understanding of method overloading, this write-up demonstrates the following concepts in detail:

  1. What is Method Overloading
  2. Various ways of Overloading a Method
  3. Implementation Method Overloading in Java

So, let’s get started.

What is Method Overloading

It is a process of creating more than one method with the same name but with different parameters. In java, the concept of method overloading is also referred as the run time or compile-time polymorphism.

Various ways of Overloading a Method

In java, a method can be overloaded either if the number of arguments is different or the methods have different data types. The below examples will provide a better understanding of the method overloading:

Example

The above-given snippet shows an error because both the methods have the same data types, the same parameter number, and the same order of parameters.

Example
Changing the order of parameters allows us to create multiple methods with the same name:

Example

Method overloading allows us to create two methods with the same names, but with a different number of parameters as shown in the below-given snippet:

Example

Using method overloading, we can also create multiple methods with the same names and the same number of parameters but with different return data types:

Let’s move step further to implement all the above-mentioned concepts practically in Java.

Implementation of Method Overloading in Java

The following code creates two methods with the same name to add the two numbers of different data types i.e. integer values and float values respectively:

static int sum( int num1, int num2){
  return num1+num2;
}
static float sum( float n1, float n2){
  return n1+n2;
}

Now, we will pass the values while calling the methods:

public static void main(String[] args) {
  int result1 = sum(50, 75);
  float result2 = sum(104.20f, 176.26f);
  System.out.println("The Sum of Integer Values : " + result1);
  System.out.println("The Sum of Float Values : " + result2);
 }

The below-given snippet shows the complete code along with the output:

The above output verifies that although we created both methods with the same name, however, both the methods are called according to their respective values i.e. when we passed the float values then the sum method having float parameters execute, and when we passed the integer values then the sum method having integer parameters execute.

Conclusion

Method overloading allows a programmer to create more than one method with the same name in three different ways i.e. number of parameters should be different, the number of data types should be different, or the order of the parameters should be different. This write-up provides a detailed guideline for what is method overloading, the different ways of using method overloading, and practical implementation of the method overloading using different examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.