Method Overloading in Java

Method overloading in Java

Well, we have come across some methods like abs(from the Math class), or equals(from the Arrays class), which had different variants for covering data of different data types. These are some examples of the concept of method overloading.

So, if we normally try to create methods with the same names and same parameters in the same scope, we won’t be able to do that. But, if we change the method signature, we are allowed to use the method with the same name in the class.

Basically, the method signature is a part of the method declaration. It is a combination of the method name and the parameter list. Now it is clear that we are not going to change the name of the methods because we need to keep them the same. So, if we change the parameter list, we can write the method with the same name in the class.

Let’s have an example, where we will be just adding two values.

The method name is added, and the parameters can be two integers, two floating-point numbers, two double values, two long values, or even two Strings(in this case, we will concatenate the strings). So, with different parameters, it is possible to create two or more methods with the same name. This is known as method overloading. So, within the same class, we will be writing methods with the same names, but different parameters.

One thing is to be noted that the return type of a method is not the part of the signature. So it is not like we will create two methods with the same name, same parameter list, and just change the return type. This still is going to give an error. After we try the below program to understand the concept of method overloading, you may try writing two methods with the same name and parameters list, and just keep the return type of the method different, and then observe what happens.

So, now let’s get started with performing a program with which we are going to understand the concept of method overloading. Have a look at the below program –

public class MethodsExample {
//The add method is going to be overloaded.
static void add(int num1, int num2){
System.out.println(“I received int type arguments…”);
System.out.println(num1+num2);
System.out.println();
}
static void add(float num1, float num2){
System.out.println(“I received float type arguments…”);
System.out.println(num1+num2);
System.out.println();
}
static void add(double num1, double num2){
System.out.println(“I received double type arguments…”);
System.out.println(num1+num2);
System.out.println();
}
static void add(long num1, long num2){
System.out.println(“I received long type arguments…”);
System.out.println(num1+num2);
System.out.println();
}
static void add(String str1, String str2){
System.out.println(“I received String as arguments…”);
System.out.println(str1+str2);
System.out.println();
}
public static void main(String[] args) {
add(3, 4); // we are passing the integer arguments
add(56.43589f, 54.34509f); // we are passing float type arguments.
add(45.3540834098, 98.24908234849); // we are passing double type arguments.
add(340328409823L, 234823409238L); // we are passing long type arguments.
add(“String is added with “, “Another String”); // we are passing String type arguments.
}
}

So, as you can see in the above program, according to what arguments are being passed, accordingly the method executes. So, this is determined by the compile time. So, at compile-time, it is known according to the method signatures, that which method should be called.

So, this is the concept of method overloading, which allows us to write two more methods with the same name but different parameters list. This can be used Whenever and wherever needed.