Method Overloading in Java With Example

Method overloading states that we can have methods with the same name but something in their parameters should be different. If you know about constructor overloading then it is pretty similar to method overloading.

Method overloading helps us from writing the same methods under different names. For example, a method involving multiplication of 2 numbers can be named as mymultiplication(int x, int y) and the method involving multiplication of 3 numbers can have the same name with different parameters as mymultiplication(int x, int y, int z).

If method overloading would not be supported in java then we needed to create different names of methods with a different number of parameters. Taking the above example only will result in mymultiplication(int x, int y) and mymultiplication1(int x, int y, int z) methods. This can be a little confusing while understanding another person’s program.

Ways to Achieve Method Overloading in Java

1. By Changing the Number of Parameters

class MethodOverloading{

	int multiply(int x,int y) {
		int res=x*y;
		return res;
	}

	// Overloaded method with an extra parameter
	int multiply(int x,int y,int z) {
		int res=x*y*z;
		return res;
	}
}

public class Example1 {

	public static void main(String[] args) {

		MethodOverloading obj=new MethodOverloading();
		int res_two_numbers=obj.multiply(4, 5);
		int res_three_numbers=obj.multiply(2, 2,3);
		
		System.out.println("The result of multiplication between 4 and 5 is "+res_two_numbers);
		System.out.println("The result of multiplication between 2, 2 and 3 is "+res_three_numbers);
	}
}

Output:

The result of multiplication between 4 and 5 is 20
The result of multiplication between 2, 2 and 3 is 12

Code Explanation:

We have taken two classes in the above code. The first-class name is MethodOverloading and another one is Example1 which is containing the main method.

In the MethodOverloading class, we have created two functions with multiply as their names and integer as return type but with different parameters. This process is known as Method Overloading in Java.

In the Example1 class, we have defined the main method and inside it, we have created an object of MethodOverloading class. This object is calling both the functions one by one and checks whether they are executing correctly or not.

2. By Changing Datatype of Parameters

class MethodOverloading{

	void totalmarks(int x,int y) {
		int res=x+y;
		System.out.println("The total marks of the student is "+res);
	}

	// Overloaded method with change in datatype of parameters

	void totalmarks(double x,double y) {
		double res=x+y;
		System.out.println("The total marks of the student is "+res);
	}
}

public class Example2 {

		public static void main(String[] args) {
			
			MethodOverloading obj=new MethodOverloading();
			obj.totalmarks(45, 20);
			obj.totalmarks(55.6, 25.8);
		}


}

Output:

The total marks of the student is 65
The total marks of the student is 81.4

Code Explanation:

We have taken two classes in the above code. The first-class name is MethodOverloading and another one is Example2 which is containing the main method.

In the MethodOverloading class, we have created two functions with totalmarks as their names and void as return type but the data types of the parameters are different.

In the Example2 class, we have defined the main method and inside it, we have created an object of MethodOverloading class. This object is calling both the functions one by one and checks whether they are executing correctly or not.

3. By Changing Sequence of Parameters

class MethodOverloading{

	void blogdetails(int x,String s) {
		System.out.println(s+x);
	}

	// Overloaded method with change in order of parameters

	void blogdetails(String s, int x) {
		System.out.println(s+x);
	}
}

public class Example3 {

	public static void main(String[] args) {

		MethodOverloading obj=new MethodOverloading();
		obj.blogdetails(1000, "The number of People who have checked this blog are ");
		obj.blogdetails( "The number of People who have checked this blog are ",1000);
	}

}

Output:

The number of People who have checked this blog are 1000
The number of People who have checked this blog are 1000

Code Explanation:

We have taken two classes in the above code. The first-class name is MethodOverloading and another one is Example3 which is containing the main method.

In the MethodOverloading class, we have created two functions with blogdetails as their names and void as return type but the sequence of the parameters are different.

In the Example3 class, we have defined the main method and inside it, we have created an object of MethodOverloading class. This object is calling both the functions one by one and checks whether they are executing correctly or not.

Conclusion

Method Overloading in java can help you align your code in a much better way. You can also remember that overloading of the main method is possible in java. I hope you were able to understand this blog thoroughly. Please feel free to comment with your doubts.

Leave a Comment

Your email address will not be published. Required fields are marked *