Method Overloading in Java PDF
Method Overloading in Java PDF
If a class has multiple methods having same name but different in parameters, it is known as Method
Overloading.
If we have to perform only one operation, having same name of the methods increases the readability
of the program.
Suppose you have to perform addition of the given numbers but there can be any number of
arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three
parameters then it may be difficult for you as well as other programmers to understand the behavior
of the method because its name differs.
In this example, we have created two methods, first add() method performs addition of two numbers
and second add method performs addition of three numbers.
In this example, we are creating static methods so that we don't need to create instance for calling
methods.
class Adder{
class TestOverloading1{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Output:
22
33
In this example, we have created two methods that differs in data type. The first add method receives
two integer arguments and second add method receives two double arguments.
1. class Adder{
4. }
5. class TestOverloading2{
7. System.out.println(Adder.add(11,11));
8. System.out.println(Adder.add(12.3,12.6));
9. }}
Output:
22
24.9
Method Overloading
With method overloading, multiple methods can have the same name with different parameters
Example
int myMethod(int x)
float myMethod(float x)
Consider the following example, which has two methods that add numbers of different type:
Example
return x + y;
return x + y;
}
Instead of defining two methods that should do the same thing, it is better to overload one.
In the example below, we overload the plusMethod method to work for both int and double:
Example
return x + y;
return x + y;
Method overloading is a concept that allows to declare multiple methods with same name but different
parameters in the same class.
Java supports method overloading and always occur in the same class(unlike method overriding).
Method overloading is one of the ways through which java supports polymorphism. Polymorphism is a
concept of object oriented programming that deal with multiple forms.
Method overloading can be done by changing number of arguments or by changing the data type of
arguments.
If two or more method have same name and same parameter list but differs in return type can not be
overloaded.
Note: Overloaded method can have different access modifiers and it does not have any significance in
method overloading.
This is a general issue when working with objects, if same name methods having reference type
parameters then be careful while passing arguments.
Below is an example in which you will know how a null value can cause an error when methods
are overloaded.
Example:
Null value is a default value for all the reference types. It created ambiguity to JVM that reports
error.
System.out.println("test(Integer ) ");
System.out.println("test(String ) ");
obj.test(null); }}