Method Overloading in Java with examples
Method Overloading is a feature that allows a class to have more than
one method having the same name, if their argument lists are different.
let’s get back to the point, when I say argument list it means the
parameters that a method has: For example the argument list of a method
add(int a, int b) having two parameters is different from the argument
list of the method add(int a, int b, int c) having three parameters.
Three ways to overload a method
In order to overload a method, the argument lists of the methods must
differ in either of these:
1. Number of parameters.
For example: This is a valid case of overloading
add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:
add(int, int)
add(int, float)
3. Sequence of Data type of parameters.
For example:
add(int, float)
add(float, int)
Invalid case of method overloading:
When I say argument list, I am not talking about return type of the
method, for example if two methods have same name, same parameters
and have different return type, then this is not a valid method
overloading example. This will throw compilation error.
int add(int, int)
float add(int, int)
Method overloading is an example of Static Polymorphism.
Points to Note:
1. Static Polymorphism is also known as compile time binding or early
binding.
2. Static binding happens at compile time. Method overloading is an
example of static binding where binding of method call to its definition
happens at Compile time.
Method Overloading examples
As discussed in the beginning of this guide, method overloading is done
by declaring same method with different parameters. The parameters
must be different in either of these: number, sequence or types of
parameters (or arguments). Lets see examples of each of these cases.
Argument list is also known as parameter list
Example 1: Overloading – Different Number of parameters in
argument list
This example shows how method overloading is done by having
different number of parameters
class DisplayOverloading
{
public void disp(char c)
{
[Link](c);
}
public void disp(char c, int num)
{
[Link](c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
[Link]('a');
[Link]('a',10);
}
}
Output:
a
a 10
In the above example – method disp() is overloaded based on the
number of parameters – We have two methods with the name disp but
the parameters they have are different. Both are having different number
of parameters.
Example 2: Overloading – Difference in data type of parameters
In this example, method disp() is overloaded based on the data type of
parameters – We have two methods with the name disp(), one with
parameter of char type and another method with the parameter of int
type.
class DisplayOverloading2
{
public void disp(char c)
{
[Link](c);
}
public void disp(int c)
{
[Link](c );
}
}
class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
[Link]('a');
[Link](5);
}
}
Output:
a
5