0% found this document useful (0 votes)
20 views

Java Method Overloding

Method overloading allows a class to have multiple methods with the same name but with different parameters. It is done by changing the number, type, or order of parameters. Some key points are that the return type cannot be used for overloading, it is an example of static polymorphism, and the parameter lists must differ in number, type, or order of parameters for overloading to work.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java Method Overloding

Method overloading allows a class to have multiple methods with the same name but with different parameters. It is done by changing the number, type, or order of parameters. Some key points are that the return type cannot be used for overloading, it is an example of static polymorphism, and the parameter lists must differ in number, type, or order of parameters for overloading to work.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Method Overloading in Java with examples

Method Overloading(compile-time polymorphism/static polymorphism/Early binding) is a feature that allows


a class to have multiple methods with the same name but with different number, sequence or type of parameters. In
short multiple methods with same name but with different signatures. For example the signature of
method add(int a, int b) having two int parameters is different from signature of method add(int a, int b, int
c) having three int parameters.

float add(int a, float b);

float add(int a,float b,int c);

float add(float a,int b);

float add(float a, float b);

Valid Overloading Signature

int abc(int num);

float abc(int num);

Invalid Overloading Signature

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.

Three ways to overload a method


In order to overload a method, the parameter list 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:
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.

int add(int, int) float add(int, int)


Method overloading is an example of Static Polymorphism.
Note: We will discuss polymorphism and types of it in a separate tutorial.

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.

Method Overloading examples

As discussed in the beginning of this guide, method overloading is done by declaring same method with different
signatures. Let’s see some examples:

1: Example 1: Overloading – Different Number of parameters in signature

2: Example 2: Overloading – Data type of parameters are different

3: Example3: Overloading – Sequence of data type of parameters is different

Example 1: Overloading – Different Number of parameters in signature

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

Example 2: Overloading – Data type of parameters are different


In this example, we are overloading the method add() based on the data type of parameters. We have two
methods with the name add() but the type of parameters are different. The first variation of add() has two int
params while the second variation of method add() has two float params.

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();

// This will call the method where first parameter is int


// and the second parameter is float
System.out.println(obj.add(10, 10.5f));

// This will call the method where first parameter is float


// and the second parameter is int
System.out.println(obj.add(1.5f, 1));
}}
Output:
Advantages of Method Overloading

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.

You might also like