Java Method Overloding
Java Method Overloding
This is one of the most popular OOP feature in java, there are several cases where we need more than one
methods with same name. For example let’s say we are writing a java program to find the sum of input numbers,
we need different variants of add method based on the user inputs such as add(int, int), add(float, float) etc.
It is similar to constructor overloading in Java, that allows a class to have more than one constructor with
different argument lists.
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:
Parameters list doesn’t mean the 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 a
compilation error.
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.
Argument list vs parameter list: Argument list and parameter list are same but they are used in different context,
when we declare a method, the parameters are called parameter list, while calling the method the argument we pass
are called argument list.
As discussed in the beginning of this guide, method overloading is done by declaring same method with different
signatures. Let’s see some examples:
This example shows how method overloading is done by having different number of parameters. In this
example, we have two methods with the same name add, but number of parameters are different. First variation
of add() method has two int parameters, while the second variation of method add() has three int parameters.
class DisplayOverloading{
//adding two integer numbers
int add(int a, int b)
{
int sum = a+b;
return sum;
}
//adding three integer numbers
int add(int a, int b, int c)
{
int sum = a+b+c;
return sum;
}}
class JavaExample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}}
Output:
30
60
class DisplayOverloading2{
//two int parameters
public int add(int a, int b)
{
int sum = a+b;
return sum;
}
//two float parameters
public float add(float a, float b)
{
float sum = a+b;
return sum;
}}
class JavaExample1{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
//This will call the method add with two int params
System.out.println(obj.add(5, 15));
//This will call the method add with two float params
System.out.println(obj.add(5.5f, 2.5f));
}}
Output:
20
8.0
Example3: Overloading – Sequence of data type of parameters is different
In this example, the both the variations of method add() has same number of parameters but the sequence of data
type of parameters is different.
First variation has (int, float) parameters and the second variation has (float, int) parameters. Since the sequence is
different, the method can be overloaded without any issues.
class DisplayOverloading3{
public float add(int a, float b)
{
System.out.println("Method with (int, float) param list.");
return a+b;
}
public float add(float a, int b)
{
System.out.println("Method with (float, int) param list.");
return a+b;
}}class JavaExample3{
public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
Readability: It is not possible to have two methods with same name unless you are doing method
overloading. Constantly looking for a unique method name for a similar task is quite a headache. It also
impact the readability of the code. Method overloading solves this problem and improves readability of the
code.
Structured Program: Improves the structure of overall program. Promotes the use of methods which
reduces the lines of code significantly.
Reusability: Methods promotes reusability. Instead of writing same code again and again, define the code
in method and call it whenever required.
Reduces code size: It reduces the number of lines of the code because of the improved program structure.