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

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

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

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 Reply

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