Method Overloading in Java

In every Java program, we have used many components to make our code much more efficient and reliable, some aspects were handy while in particular situations, where as some were the basic essential component of every Java program which supported several functionalities. Similar was the case with the methods, In the previous tutorial we only learnt about the basics of Declaring methods, calling them etc. But besides all those, there still lies some functionalities, which make this concept of methods in Java much more useful and reliable.

In this tutorial, we'll be discussing about the concept of Method Overloading in Java with the help of certain real life scenarios and examples.


What is Method Overloading in Java?

If a Java Class has 1 or more methods with the same name but with different set of parameters(differing in number of parameters or the types of parameters), then it is known as Method Overloading. And such Java methods are known as Overloaded methods. Method Overloading is useful when there is a need to perform same semantic operations just with a little difference in parameter number/type.

For example: 
void add(int a, int b);
void add(int a, int b, int c);
void add(float a, float b);

In all these examples, the add() method is overloaded. You can notice in the first method, we have 2 parameters, and then 3 parameters and then different type of parameters. The compiler will automatically call the required method, according to the number of arguments passed and their data types.

Let's see this with a live example:

public class methodOverloadingExample{
public static void main(String args[]){
A obj = new A();
obj.add(2,1);
System.out.print(obj.add(1.1,2.1));
obj.add(1,2,3);
}
}
class A{
public void add(int a, int b){
System.out.print(a+b);
}
public void add(int a, int b, int c){
System.out.print(a+b+c);
}
public float add(float a, float b){
return a+b;
}
}

OUTPUT
3
3.2
6

Here when we call the add() method with both integer arguments, the compiler automatically calls the add(int a, int b) method. And when we call the add() with decimal values as arguments. The other function is called. 

* An important point to note here is, that return type don't play any role in achieving method overloading. An overloaded method may or may not have same return type. Method overloading is only achieved in case of variable number of arguments or different types of arguments.

Ways to achieve Method Overloading in Java

Method Overloading can be achieved by only 3 methods:

1. Changing the number of parameters: When 2 or more methods having same name, but different number of parameters then they are said to be Overloaded. 

public class methodOverloadingExample1{
    public static void main(String args[]){
    A obj = new A();
    obj.add(2,1);
    obj.add(1,2,3);
    System.out.print(obj.add(1));
    }
    }
    class A{
    public void add(int a, int b){
    System.out.print(a+b);
    }
    public void add(int a, int b, int c){
    System.out.print(a+b+c);
    }
    public int add(int a){
    return a;
    }
    }

OUTPUT
3
6
1

The add() method here is overloaded with different number of parameters. 

2. By changing the type of parameters: This way is quite useful in many contexts, together with the concept of Type Promotion, we can make the program even more effective. The methods here, need to have to just different data types, even if the number of parameters is same.

Example: 

public class methodOverloadingExample1{
    public static void main(String args[]){
    A obj = new A();
    obj.add(2,1);
    obj.add(1,2,3);
    obj.add(1,2,2,2);
    obj.add(1.1,2.2);
    }
    }
    class A{
    public void add(int a, int b){
    System.out.print(a+b);
    }
    public void add(int a, int b, int c){
    System.out.print(a+b+c);
    }
    public void add(int a, long b, long c, long d ){
    System.out.print(a+b+c+d);
    }
    public void add(float a, float b){
    System.out.print(a+b);
    }
    }

OUTPUT
3
6
7
3.3

When we call the method with 4 parameters, the int value is automatically converted into the long data type, due to Type promotion. And we get the result as usual. Type promotion can occur from small data types to larger ones. We can also do the reverse, convert a larger data type in to a smaller one, using explicit type casting.

3. By changing the order of the parameters in the method: If both the methods receive exactly same number of parameters with even same data types. Then Method overloading can be achieved by switching the positions/sequence of the parameters.

Example: 
void funct(int a, float b);
void funct(float a, int b);

Program: 
public class methodOverloadingExample1{
    public static void main(String args[]){
    A obj = new A();
    obj.func(2,'a');
    obj.func('a',2);
    }
    }
    class A{
    public void func(int a, char b){
    System.out.print("In First Method");
    }
    public void func(char b, int a){
    System.out.print("In Second method");
    }
    }

OUTPUT
In First Method
In Second method

Is it possible to have different return types for Overloaded methods in Java?

It is basically impossible to have method overloading in Java with the change of return type, because of ambiguity or confusion among the compiler. If we call a method having same name and same number/type of parameters but having different return types, then the compiler would not be able to decide which method to call, and thus it would give a compile-time error

For method overloading, there should be a change in the method signature, and return type does not count in method signature.

Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post