Method Overloading in Java

Method Overloading in Java Programming Language is nothing but defining two or more methods with the same name in a class. Java allows us to assign the same name to multiple definitions, as long as they hold a unique set of arguments or parameters and called method overloading. This uniqueness helps the compiler differentiate between one method and the other. So, it can call the appropriate one based on the parameters.

We have already seen many Java method overloading techniques, but you may not have noticed. For example, the Arrays.Sort function that we explained in our previous article or System.out.println function, which accepts multiple data types as a parameter. The method overloading can be achieved by

  • Changing the Number of arguments. Please refer to example 1.
  • They should be a change in the data type of at least one argument. Please refer to examples 2, 3, and 4.

Javac does not consider the following definitions as Java Method overloading perhaps it will throw an error:

  • Changing the argument names. Please refer to example 6.
  • Change the return type of a function. Please refer to example 7.

Java Method Overloading Example

This program will show how to perform Method Overloading by changing the number of arguments. To do so, First, we created a Class, and within the class, we defined two functions with the same name, but we changed the number of arguments.

public class Example {
	public void sum(int a, int b) { // F1
		System.out.format("The Sum of Two Numbers is = %d \n", (a + b));
	}
	
	public void sum(int a, int b, int c) { //F2
		System.out.format("The Sum of Three Numbers is = %d", (a + b + c));
	}
}

Within the main of this Java method overloading, we are creating the Object/instance of the above class and calling the functions inside it.

public class FindTotal {
	public static void main(String[] args) {
		Example obj = new Example();
		obj.sum(10, 20); // Calling F1
		obj.sum(10, 20, 40); // Calling F2
	}
}
Method Overloading in Java 1

Within this method overloading program, the following statement allows us to create an instance of the Example class.

Example obj = new Example();

Since we passed two arguments to the sum, the compiler will call the first one: public void sum(int a, int b)

obj.sum(10, 20);

Next line, we passed three arguments to the sum. So the compiler will call the second one: public void sum(int a, int b, int c)

obj.sum(10, 20, 40);

Method Overloading in Java Example 2

In this program, we will show how to perform Method Overloading by changing the data type of arguments. In order to do so, First, we created a Class, and within the class, we defined three functions with the same name, but we changed the data type of both arguments.

public class Simple {
	public void add(int a, int b) { //function F1
		System.out.format("The Sum of Two Integer Values = %d \n", (a + b));
	}	
	public void add(float a, float b) { //function F2
		System.out.format("The Sum of Two Float Values = %.4f \n", (a + b));
	}
	public void add(String str1, String str2) { //function F3
		System.out.println("The Sum of Three Numbers is = " + (str1 + str2));
	}
}

Within the main program of a method overloading, we are creating the Object/instance of the above class. It allows us to call the functions inside the Sample.

public class Sample {
	public static void main(String[] args) {
		Simple obj = new Simple();
		obj.add(10, 20); // Calling F1
		obj.add(20.34f, 40.92f); // Calling F2
		obj.add("Tutorial ", "Gateway"); // Calling F3
	}
}
The Total of Two Integer Values = 30 
The Total of Two Float Values = 61.2600 
The Total of Three Numbers is = Tutorial Gateway

The first statement in the method overloading allows us to create an instance of the Simple class.

Since we passed two integer values to the add, the compiler will call the first one: public void add(int a, int b)

obj.add(10, 20);

Next line, we passed two float values to the add. So the compiler will call the second: public void add(float a, float b)

obj.add(20.34f, 40.92f); 

Next line of the example, we passed two strings to the add. Here, the compiler will call the third one: public void add(String str1, String str2) to perform string concatenation.

obj.add("Tutorial ", "Gateway"); 

Method Overloading in Java Example 3

In our previous example of method overloading, we changed the data type of all the available parameters. In this program, we will show what happens when we change the data type of a single argument. And What happens when we change the order of the arguments?

In order to do so, First, we created a Java Class, and within the class, we defined three functions with the same name. However, we changed the data type of single arguments and the order of the arguments.

public class Simple {
	public void add(int a, int b) { //function F1
		System.out.format("The Sum of Two Integer Values = %d \n", (a + b));
	}	
	public void add(int a, float b) { //function F3
		System.out.format("The Sum of Int & Float Values = %.4f \n", (a + b));
	}
	public void add(float a, int b) { //function F3
		System.out.format("The Sum of Float & Int Values = %.4f \n", (a + b));
	}
}

Within the example of Java Method Overloading main, we are creating the Object/instance of the above class. It allows us to call the functions inside the Sample.

public class Sample {
	public static void main(String[] args) {
		Simple obj = new Simple();
		obj.add(10, 20); // Calling F1
		obj.add(30, 40.92f); // Calling F2
		obj.add(60.98f, 20); // Calling F3
	}
}
The Sum of Two Integer Values = 30
The Sum of Int & Float Values = 70.9200
The Sum of Float & Int Values = 80.9800

In this example, the first statement allows us to create an instance of the Simple class.

Since we passed two integer values to the add, the compiler will call add(int a, int b)

obj.add(10, 20); // F1

Next line, we passed one integer value and a float value to the add. So the compiler will call add(int a, float b)

obj.add(30, 40.92f); // F2

Next line, we passed one float value and an integer value to the add. So the compiler will call the third one add( float a, int b)

obj.add(60.98f, 20); // F3

Example 4

In this Java program, we will show how it will implicitly do the typecasting while Method Overloading. To do so, first, we created a Class, and within the class, we defined two functions with the same name. However, we changed the data type of the second function argument.

Remember, we used three parameters in the second one to understand easily, and if you want two arguments, then stick to two.

public class Simple {
	public void add(int a, int b) { //F1
		System.out.format("The Sum of Two Integer Values = %d \n", (a + b));
	}	
	public void add(int a, long b, long c) { //F2
		System.out.format("The Sum of Int & Long Values = %d \n", (a + b + c));
	}
}

Within the main of this Method Overloading, we are creating the Object/instance of the above class. It allows us to call the functions inside the Sample.

public class Sample {
	public static void main(String[] args) {
		Simple obj = new Simple();
		obj.add(15, 45); // Calling F1
		obj.add(30, 90, 220); // Calling F2
	}
}
The Sum of Two Integer Values = 60
The Sum of Int & Long Values = 340

The following statement allows us to create an instance of the Simple class.

Simple obj = new Simple();

Since we passed two integer values to the add, the compiler will call the first one: dd(int a, int b)

obj.add(15, 45); 

Next line, we passed one integer value and two long values to the add. So the compiler will call the second: public void add(int a, long b, long c). Here, the function will implicitly convert the integer value to long and perform the addition operation.

obj.add(30, 90, 220); 

Example 5

In this program, we will show what happens when we interchange the data type of arguments?. For this Java Method Overloading example, First, we created a Class. Within the class, we defined two functions with the same name, but we interchanged the data type of arguments.

public class Simple {
	public void add(int a, long b) { //function F1
		System.out.format("The Output from First = %d \n", (a + b));
	}	
	public void add(long a, int b) { //function F2
		System.out.format("The Output from Second = %d \n", (a + b));
	}
}

Within the main program of method overloading, we are creating the Object/instance of the above class. It allows us to call the functions inside the Sample.

public class Sample {
	public static void main(String[] args) {
		Simple obj = new Simple();
		obj.add(15, 45); 
	}
}
Method Overloading in Java 5

Within this example, the below statement allows us to create an instance of the Simple class.

Simple obj = new Simple();

Here the compiler will not understand which one of them it has to call. Because both the first: public void add(int a, long b)and the second function: public void add(int a, long b) is true. That is why it will throw an error stating that the function is Ambiguous.

obj.add(15, 45);

Example 6

In this Java method overloading program, we will show what happens when we change the argument names. In order to do so, First, we created a Class, and within the class, we defined two functions with the same name and same arguments. However, we changed the argument names from a to x and b to y.

public class Simple {
	public void add(int a, int b) { 
		System.out.format("The Output from First = %d \n", (a + b));
	}	
	public void add(int x, int y) { 
		System.out.format("The Output from Second = %d \n", (a + b));
	}
}

Stating that this is not, and it cannot be encouraged.

Method Overloading in Java 6

In this program, we will show what happens when we change the return type. In order to do so, First, we created a Class, and within the class, we defined two functions with the same name and same arguments, but we changed the return type to double.

public class Simple {
	public int add(int a, int b) { //function F1
		int c = a + b;
		return c;
	}	
	public double add(int a, int b) { //function F2
		double c = a + b;
		return c;
	}
}

It states that this is not overloading and cannot be encouraged.

Duplicate Name Error Message