Java Overloading With Examples

What is Overloading?

The feature in Java that allows a class to have multiple methods with the same name is called Overloading. Each version is distinct with variation in its signature. Overloading is also applicable to constructors. Overloading is also known as static/compile-time polymorphism.

The purpose of overloading is to provide convenience to the coder and reader thereby increasing its readability.

Let us understand with an example:

We have a list of function along with their purpose:

Method Purpose
int add2Int(int x,int y) Adds two integers
float add2Float(float x,float y) Adds two floats
int add3Int(int x,int y,int z) Adds three integers
float add3Float(float x,float y,float z) Adds three floats

All these methods serve the common purpose i.e adding. From the development perspective, it is not feasible to remember every name distinctly. Instead, we can have just one name common for all i.e add. Each method version however will have a different signature but serving a common motive.

What is the signature?

The signature consists of the method name and the parameter list. Note that the return type is not part of the signature.

<method-name>(<parameter-list>)

Ways of overloading

The overloading as mentioned above can be done by having a different signature for each variation. Since the method name is the same, so we need to alter the parameter-list. We can do it in any one of the following ways:

  • Different number of parameters
  • Different data-type of parameters
  • Different order of parameters

 Let us discuss each of these ways.

Different number of parameters

If two methods have the same name with a different number of parameters, overloading is achieved.

Example:

We have two add methods. One has two input parameters, other has three input parameters.

//Version 1 : Accepts 2 parameters
int add(int x, int y){
System.out.println("I am add method that accepts 2 parameters");
return( x+y);
}
//Version 2 : Accepts 3 parameters
int  add(int x, int y, int z) {
System.out.println("I am add method that accepts 3 parameters");
return( x+y+z);
}
class MyOverloading{
//Version 1 : Accepts 2 parameters
int add(int x, int y){
System.out.println("I am add method that accepts 2 parameters");
return( x+y);
}
//Version 2 : Accepts 3 parameters
int add(int x, int y, int z) {
System.out.println("I am add method that accepts 3 parameters");
return( x+y+z);
}
}

public class ClassMain{
public static void main(String args[]){
MyOverloading obj=new MyOverloading();
int xy=obj.add(23,25);
System.out.println("23 + 25 = "+xy);
int xyz=obj.add(23,25,75);
System.out.println("23 + 25 + 27 = "+xyz);
}
}

Output:

I am add method that accepts 2 parameters
23 + 25 = 48
I am add method that accepts 3 parameters
23 + 25 + 27 = 123

Different data-type of parameters

If two methods have the same name and different data types of parameters, overloading is achieved.

Example:

We have two add methods. One has two int input parameters, other two float input parameters.

int add(int x, int y){
System.out.println("I am add method that accepts 2 int parameters");
return(x+y);
}

float add(float x, float y) {
System.out.println("I am add method that accepts 2 float parameters");
return(x+y);
}
}
class MyOverloading{
int add(int x, int y){
System.out.println("I am add method that accepts 2 int parameters");
return(x+y);
}

float add(float x, float y) {
System.out.println("I am add method that accepts 2 float parameters");
return(x+y);
}
}

public class ClassMain{
public static void main(String args[]){
MyOverloading obj=new MyOverloading();
int xyInt=obj.add(23,25);
System.out.println("23 + 25 = "+xyInt);
float xyFloat=obj.add(11.34f,22.45f);
System.out.println("11.34 + 22.45 = "+xyFloat);
}
}

Output:

I am add method that accepts 2 int parameters
23 + 25 = 48
I am add method that accepts 2 float parameters
11.34 + 22.45 = 33.79

Different order of parameters

If two methods have the same name but different ordering of parameters, overloading is achieved.

Example:

We have two printDetails methods. They are:

//Version 1
void printDetails(int x,String y){
System.out.println("Version 1: My 1st parameter is int and 2nd parameter is String:"+x+" "+y);
}
//Version 2
void printDetails(String x,int y){
System.out.println("Version 2:My 1st parameter is String and 2nd parameter is int:"+x+" "+y);
}

We can see that the ordering of the parameter list is different. So this is one more way of achieving polymorphism.

class MyOverloading{
//Version 1
void printDetails(int x,String y){
System.out.println("Version 1: My 1st parameter is int and 2nd parameter is String:"+x+" "+y);
}
//Version 2
void printDetails(String x,int y){
System.out.println("Version 2: My 1st parameter is String and 2nd parameter is int:"+x+" "+y);
}

}

public class ClassMain{
public static void main(String args[]){
MyOverloading obj=new MyOverloading();
obj.printDetails(25,"Madhu");   //It calls Version1
obj.printDetails("Gouri",11);   //It calls Version2
}
}

Output:

Version 1: My 1st parameter is int and 2nd parameter is String:25 Madhu
Version 2: My 1st parameter is String and 2nd parameter is int:Gouri 11

It must be noted that the compiler does not allow two methods to have the same signature in a single class.

int multiply(int x,int y);
float multiply(int x,int y);

The above combination is not a valid situation. The signature is the same, only the return type is different. So this will result in a compile-time error.

Static Polymorphism

We mentioned Overloading to be a static polymorphism. At the time of compiling only, it is known about which version of a method we intend to call due to differences in the signature. Post compilation, the binding to version does not change, hence called static. It is also referred to as early binding.

Type Promotion

In case we call a method with a certain set of parameters and there is no exact match, Java performs an implicit process of type promotion. It means widening the data to find any compatible match. By widening, we refer to shifting to a higher size data type.

This shows the possible widening roadmap.

So short can be promoted to int in order to look for a compatible match.

Despite the promotion, if no match is available, it results in compiler time error.

This was an overview of Java Overloading. We will discuss Overriding in the next lecture.