As you know that the purpose of a method is to exhibit a behavior. For example, when you apply a brake in any vehicle, it stops the vehicle. The method brake() represents the behavior of stopping the vehicle. Car brake is applied by the leg and the cycle brake is applied by hand. The way of operating is different but the desired behavior is one and the same. Similarly, if the behavior of more than one methods with the same name does the same thing with varying numbers of parameters or varying types of parameters, it is called overloading
Method overloading in Java is a very useful feature for increasing the maintainability and readability of code. It should be used with caution.

Method Overloading in Java


When a class has more than one method having the same name but different in parameters, it is called method overloading. In other words, when multiple methods are declared with the same name but with different parameter list, all these methods are said to be overloaded. The signature of a method is defined by its name and its list of parameters. It shares a common name with a different parameter list. The return type of a method is not the part of the method signature. It does not play any role in resolving methods overloaded.
When a class has multiple methods having the same name with the different parameter list, the compiler differentiates one method from another method by checking the different number of parameters or types of parameters. The compiler does not consider the return type while differentiating the overloaded method.
Java method overloading with example programs

Method overloading rules in Java


Here is the list of rules by which method overloading can be implemented in Java. They are as follows:
1. The method name must be the same.
2. Parameters must be different i.e each overloaded method must take a unique list of parameter types. The parameters can be changed in one of the following three ways.
a. Data types of parameters.
For example, This is the valid case of method overloading.
    void add(int a, int b); // There are two parameters a and b with data type int. void add(int a, float b); //Here, two parameters a, and b with data type int and float. The data type is different in both cases.
b. Number of parameters.
For example:
    void sum(int a, int b); // Two parameters a and b with data type int. void sum(int a, int b, int c); // Three parameters a, b, and c with data type int. Here, the number of parameters are different but the data type is the same.
c. Sequence of the data type of parameters.
For example:
    void sub(int a, double b); void sub(double a, float a);
3. Access specifiers can be anything or different.
4. Return type can be anything or different.
5. Exception thrown can be anything.
6. The implementation does not matter in this case. A method can have any kind of logic.

Features of the method overloading in Java


There are following features of the method overloading in java which you should have to keep in mind.
1. The call to the overloaded method is bonded at compile time.
2. The concept of method overloading is also known as compile-time polymorphism in java.
3. Method overloading is generally done in the same class. But it can also be done in the subclass. You will have to make a relationship between the parent class and child class by using extends keyword for it. 
4. Method overloading in Java cannot be done by changing the return type of the method because there may occur ambiguity. But the overloaded methods can change the return type.
5. The private methods can be overloaded in Java.
6. The final methods can be overloaded in Java.
7. The main method can also be overloaded in Java.
8. Both static and instance method can be overloaded in Java. Method overloading is possible when two or more static methods with the same name, but difference in the list of parameters.

Why method overloading is done or used in Java


1. Method overloading is done to reuse the same method name.
2. It is done to make the program logically more readable and understandable.
3. It is used to achieve the compile-time polymorphism in Java. 

Method overloading example programs in Java


Let's see some practical example programs related to the method overloading in Java.

Method overloading by changing the number of arguments


In this example program, we will create two methods, the first method sum() will perform the addition of two numbers and the second method sum() will perform the addition of three numbers by overloading. let's see the program source code.
Program source code 1: 
    package methodOverloading; public class Addition { // Method to calculate the sum of two numbers. // Declare an instance method sum with two parameters a and b having data type int. void sum(int a, int b){ int s=a+b; System.out.println("Sum of two numbers: " +s); } // Method to calculate the sum of three numbers. // Declare an instance method sum with three parameters a,b, and c having data type int. // Here, the method sum() is overloaded having the same method name. void sum(int a, int b, int c){ int t=a+b+c; System.out.println("Sum of three numbers: " +t); } public static void main(String[] args) { Addition a=new Addition(); a.sum(10, 20); // It will call sum() method to calculate the sum of two arguments. a.sum(50, 100, 200); // It will call sum() method to calculate the sum of three arguments. } }
    Output: Sum of two numbers: 30 Sum of three numbers: 350
As you can see in the above example program, the method sum() is overloaded based on the number of parameters. We have declared two methods having the same name but the different number of parameters. The overloaded methods have calculated the sum of two numbers and three numbers based on the passing of arguments while calling.

Method overloading by changing the data type of parameters


In this example program, we have created two methods having the same name but differ in the data type of parameters. The first method sub() will receive two integer arguments and the second method sub() will receive two double arguments.
Program source code 2:
    package methodoverload; public class Subtraction { // Declare an instance method sub with two parameters x and y having data type int. int sub(int x, int y){ int a=x-y; System.out.println("Subtraction of two numbers: " +a); return a; } // Declare an instance method sub with two parameters x and y having data type double. double sub(double x, double y){ double b=x-y; System.out.println("Subtraction of two numbers: " +b); return b; } public static void main(String[] args) { Subtraction s=new Subtraction(); s.sub(6, 5); // It will call the method sub() to calculate the subtraction of two int type arguments. s.sub(20.8, 10.9); // It will call the method sub() to calculate the subtraction of two double type arguments. } }
    Output: Subtraction of two numbers: 1 Subtraction of two numbers: 9.9
In the above programs, the method sub() is overloaded based on the data type of parameters. We have declared two methods having the same name but different data type of parameters. The overloaded methods have calculated the subtraction of two numbers based on the data type of the parameters.

Method overloading by changing the sequence of data type of parameters


Let's see a simple method overloading example program based on the changing of the sequence of the data type of parameters.
Program source code 3:
    package methodOverloading; public class Multiplication { void multiply(int a, double b){ double m1=a*b; System.out.println("Multiplication of two numbers: " +m1); } void multiply(double a, int b){ double m2=a*b; System.out.println("Multiplication of two numbers: " +m2); } public static void main(String[] args) { Multiplication m=new Multiplication(); m.multiply(10, 20.5); // It will call the method multiply() to calculate the multiplication of two argumnets int and double. m.multiply(10.5, 30); // It will call the method multiply() to calculate the multiplication of two arguments double and int. } }
    Output: Multiplication of two numbers: 205.0 Multiplication of two numbers: 315.0
In the above program, the method multiply() is overloaded based on the sequence of the data type of parameters. The overloaded methods have calculated the multiplication of two numbers based on sequence of the argument types while calling. 


Method overloading done in the Subclass


In this example program, method overloading will be done in the child class or subclass. We have made a relationship between the parent class and child class by using extends keyword. We have created one method msg() in the parent class and the second method msg() in the child class or subclass. while calling from the class 'Test', the msg() will receive int argument and double argument. Let's see the source code.
Program source code 4:
    package methodOverloading; public class SuperClass { void msg(int x, int y){ System.out.println("Hello Java"); } } package methodOverloading; public class Subclass extends SuperClass { void msg(double x, double y){ System.out.println("Welcome you in Java programming"); } } package methodOverloading; public class Test { public static void main(String[] args) { Subclass sc=new Subclass(); sc.msg(10, 20); sc.msg(2.5, 3); } }
    Output: Hello Java Welcome you in Java programming

Why method overloading is not possible by changing the return type of method?


In Java, Method overloading cannot be done when the return type, method name, and argument list are the same because there may occur ambiguity. Let's see how ambiguity may occur:
Program source code 5:
    package methodOverloading; public class ReturnTypeEx1 { public int m1(int a, int b) // Duplicate method error. { int x=a+b; return x; } public int m1(int c, int d) // Duplicate method error. { int y=c*d; return y; } } package methodOverloading; public class ReturntypeTest { public static void main(String[] args) { ReturnTypeEx1 obj=new ReturnTypeEx1(); int sum=obj.m1(20, 30); System.out.println(sum); int multiply=obj.m1(20,30); System.out.println(multiply); } }
    Output: 50
As you can see the above program output, method overloading cannot be done because compiler shows the duplicate method error.

Program source code 6: Return type is different, method name and argument list are the same.
    package methodOverloading; public class ReturnTypeEx1 { public int m1(int a, int b) // Return type is int. { int x=a+b; return x; } public double m1(int c, int d) // Return type is double. { int y=c*d; return y; } } package methodOverloading; public class ReturntypeTest { public static void main(String[] args) { ReturnTypeEx1 obj=new ReturnTypeEx1(); int sum=obj.m1(20, 30); System.out.println(sum); int multiply=obj.m1(20,30); System.out.println(multiply); } }
    Output: 50
The compiler again shows the duplicate method error because the compiler checks only method signature for duplication, not the return type. Therefore, method overloading is not possible even though their return type is different. So, the return type does not play any role in the case of method overloading

Can we overload the main() method in Java?


Yes, we can overload the main() method in Java. A class can have any number of main() methods but JVM calls that main() method that receives string array as an argument only. Therefore, the execution always starts from public static void main(String[] args) only. Let's understand a simple example related to the main method.
Program source code 7:
    public class MainMethodOverloadingTest { public static void main(String[] args) { System.out.println("main(String[] args)"); main(); } public static void main(){ System.out.println("main without args"); } public static void main(String args){ System.out.println("main with string args"); } }
    Output: main(String[] args) main without args

Valid/Invalid cases of Method overloading in Java


Let' see a few cases of the valid or invalid case of method overloading in Java.
Case 1:
    void sum(int a, int b, float c) void sum(int c, int d, float e)
Result: Compile-time error. Here, the argument lists are exactly the same. Both methods are having the same number of parameters, and the same sequence of data types of the parameters.
Case 2:

    int sum(int a, int b) int sum(double c, double d)
Result: Perfectly fine. This is the valid case of method overloading. Here. data types of arguments are different.
Case 3:
    void msg(String a, char c) void msg(char x)
Result: Perfectly fine. A valid case of method overloading. Here, the number of parameters are different.
Case 4:
    void msg() void msg(char x)
Result: This is a valid case of method overloading. This is because the number of parameters is different. There is no parameter in the first method and the second method has one parameter.
Case 5:
    void msg(Object o, char a) void msg(char x, Object o)
Result: This is a valid case of method overloading. This is because the sequence of data types of parameters is different. The first method is having (Object, char)and the second method has (char, Object).
Case 6:
    Object msg(String s) String msg(String a)
Result: Duplicate method error. Argument lists are the same. Even though the return type of methods is different. Still, it is an invalid case of method overloading. The return type of method does not play any role while overloading a method.

Advantages of Method overloading in Java


The advantages of method overloading in Java are as follows:
1. In Java, Method overloading provides the same method name to reuse in the program.
2. It increases the readability and understandable of the program.
3. It makes the program logically more readable and understandable.
4. It helps in achieving the compile-time polymorphism. 


Final words 
Hope that this article has covered the basic of method overloading in Java with some important practical example programs that are very important for beginners and freshers. I hope that you have enjoyed this article. In the next tutorial, we will discuss some more concepts of very important programs asked in the interview. 
Thanks for reading!!!!