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 method with the same name does the same thing on varying numbers of parameters or varying types of parameters, it is called overloading in java. It is one of the ways that Java implements polymorphism.
In this tutorial, we will learn Java method overloading in detail with example programs. Method overloading in Java is a powerful and very useful feature for increasing the maintainability and readability of code. It should be used with caution.
What is 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 lists, all these methods are said to be overloaded methods.
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 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.
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.
b. Number of parameters
c. Sequence of the data type of parameters
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 methods can be overloaded in Java. Method overloading is possible when two or more static methods with the same name, but the difference in the list of parameters.
When to use method overloading in Java?
Method overloading is a powerful feature in Java but should be used as per needs. It should be used when you actually need multiple methods with different parameters but methods do the same thing. If multiple methods perform different tasks, don’t use method overloading concept.
Method overloading is done in java due to the following needs:
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.
Java Method overloading Example Programs
Let’s see some practical example programs related to the method overloading in Java.
Method overloading by changing 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 take a look at the program source code related to it.
Program source code 1:
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 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:
The overloaded methods have calculated subtraction of two numbers based on the data type of parameters.
Method overloading by changing sequence of data type of parameters
Let’s take an example program that will be based on the changing of the sequence of the data type of parameters.
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
Let’s create a program where 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 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 following source code.
Program source code 4:
Why method overloading is not possible by changing 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:
As you can see the above program output, method overloading cannot be done because the compiler shows the duplicate method error.
Let’s take an example program where we will keep return type different, method name and argument list the same.
Program source code 6:
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 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 this concept by a simple example program based on the main method.
Program source code 7:
Valid/Invalid cases of Method overloading in Java
Let’ see a few cases of the valid or invalid case of method overloading in Java.
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.
Result: Perfectly fine. This is the valid case of method overloading. Here. data types of arguments are different.
Result: Perfectly fine. A valid case of method overloading. Here, the number of parameters is different.
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.
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).
Result: Duplicate method error. Argument lists are the same. Even though 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.
Advantage 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 understanding of the program.
3. It makes the program logically more readable and understandable.
4. It helps in achieving the compile-time polymorphism.
Recommended post on Method overloading
Hope that this tutorial has covered the basic of method overloading in Java with some important example programs that are very important for beginners and freshers. I hope that you will have understood this topic and enjoyed it.
Thanks for reading!!!!
⏪ PrevNext ⏩