Method Overloading in Java

A 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 method definitions, as long as they hold a unique set of arguments or parameters and called method overloading. This uniqueness helps the compiler to differentiate between one method and the other. So, it can call the appropriate methods based on the parameters.

We have already seen many Java overloading methods, but you may not have noticed. For example, the Arrays.Sort method that we explained in our previous article or System.out.println Method, which accepts multiple data types as a parameter. The Java 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 example 2, 3, and 4.

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

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

Java Method Overloading Example 1

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

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

Within the main method of this method overloading, we are creating the Object/instance of the above class and calling the methods inside the MethodOverloading1

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

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

MethodOverloading1 obj = new MethodOverloading1();

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

obj.sum(10, 20); // Calling function F1

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

obj.sum(10, 20, 40); // Calling function F2

Method Overloading in Java Example 2

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

package OopsConcept;
public class MethodOverloading2 {
	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 method of a method overloading, we are creating the Object/instance of the above class. It allows us to call the methods inside the MethodOverloading2.

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

The below statement in the Java method overloading allows us to create an instance of the MethodOverloading2 class

MethodOverloading2 obj = new MethodOverloading2();

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

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

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

obj.add(20.34f, 40.92f); // Calling function F2

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

obj.add("Tutorial ", "Gateway"); // Calling function F3

Method Overloading in Java Example 3

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

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

package OopsConcept;
public class MethodOverloading3 {
	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 method, we are creating the Object/instance of the above class. It allows us to call the methods inside the MethodOverloading3

package OopsConcept;
public class MethodOverloading3ex {
	public static void main(String[] args) {
		MethodOverloading3 obj = new MethodOverloading3();
		obj.add(10, 20); // Calling function F1
		obj.add(30, 40.92f); // Calling function F2
		obj.add(60.98f, 20); // Calling function 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 Java Method Overloading example, the following statement allows us to create an instance of the MethodOverloading3 class

MethodOverloading3 obj = new MethodOverloading3();

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

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

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

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

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

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

Method Overloading in Java Example 4

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

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

package OopsConcept;
public class MethodOverloading4 {
	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, long b, long c) { //function F2
		System.out.format("The Sum of Int & Long Values = %d \n", (a + b + c));
	}
}

Within the main method of this Java Method Overloading, we are creating the Object/instance of the above class. It allows us to call the methods inside the MethodOverloading4

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

The following statement in the method overloading in java allows us to create an instance of the MethodOverloading4 class

MethodOverloading4 obj = new MethodOverloading4();

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

obj.add(15, 45); // Calling function F1

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

obj.add(30, 90, 220); // Calling function F2

Method Overloading Example 5

In this program for method overloading in java, 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 methods with the same name, but we interchange the data type of arguments.

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

Within the main method of a method overloading in java, we are creating the Object/instance of the above class. It allows us to call the methods inside the MethodOverloading5.

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

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

MethodOverloading5 obj = new MethodOverloading5();

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 method: public void add(int a, long b) is true. That is why it will throw an error stating that the method is Ambiguous.

obj.add(15, 45);

Method Overloading 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 methods with the same name, same arguments. However, we changed the argument names from a to x and b to y.

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

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

Method Overloading in Java 6

Method Overloading Example 7

In this program, we will show what happens when we change the return type of method overloading in Java.

In order to do so, First, we created a Class, and within the class, we defined two methods with the same name, same arguments, but we changed the return type to double.

// Java Method Overloading example
package OopsConcept;
public class MethodOverloading5 {
	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 is stating that this is not overloading, and it cannot be encouraged.

Method Overloading in Java 7