Method Overloading (compile time polymorphism)

Method overloading is achieved when two or more methods in the same class have the same name but with different parameter lists. That means methods with the same name must differ in their types, number and sequence of parameters. This allows the compiler to match parameters and choose the correct method when a number of choices exist. Changing just the return type is not enough to overload a method, and will be a compile-time error. They must have a different signature.

When no method matching the input parameters is found, the compiler attempts to convert the input parameters to types of greater precision. A match may then be found without error. At compile time, the right implementation is chosen based on the signature of the method call. So, method overloading is an example of compile time polymorphism (i.e. static binding).

Advantage: Method overloading enhances the readability of the program code.

 

Example 1

Below is an example of a class demonstrating method overloading concept with respect to different parameter types.

MethodOverloading01.java

// Method overloading using different types of parameters
public class MethodOverloading01 {

//INT SUM
int sum(int a, int b) {
return a + b;
}

//DECIMAL SUM
double sum(double a, double b) {
return a + b;
}

//STRING CONCATENATION
String sum(String a, String b) {
return a + b;
}

public static void main(String[] args) {
MethodOverloading01 ob = new MethodOverloading01();
int c = ob.sum(10, 20);
double d = ob.sum(12.3, 23.2);
String e = ob.sum("Tech", "Guru");
System.out.println("INT SUM = " + c);
System.out.println("DECIMAL SUM = " + d);
System.out.println("STRING CONCATENATION = " + e);
}
}

Output:

INT SUM = 30
DECIMAL SUM = 35.5
STRING CONCATENATION = TechGuru

 

Example 2

Below is an example of a class demonstrating method overloading concept with regard to different number of parameters.

MethodOverloading02.java

// Method overloading using different numbers of parameters
public class MethodOverloading02 {

int add(int a) {
return a;
}

int add(int a, int b) {
return a + b;
}

int add(int a, int b, int c) {
return a + b + c;
}

public static void main(String[] args) {
MethodOverloading02 ob = new MethodOverloading02();
System.out.println("SUM1 = " + ob.add(10));
System.out.println("SUM2 = " + ob.add(10, 20));
System.out.println("SUM3 = " + ob.add(10, 20, 30));
}
}

Output:

SUM1 = 10
SUM2 = 30
SUM3 = 60

 

Example 3

Below is an example of a class demonstrating method overloading concept with respect to different sequence of parameters.

MethodOverloading03.java

// Method overloading using different sequence of parameters
public class MethodOverloading03 {

void show(int a, char c) {
System.out.println("a = " + a + "; c = " + c);
}

void show(char c, int a) {
System.out.println("c = " + c + "; a = " + a);
}

public static void main(String[] args) {
MethodOverloading03 ob = new MethodOverloading03();
ob.show(10, 'a');
ob.show('b', 20);
}
}

Output:

a = 10; c = a
c = b; a = 20

 

Method Overloading and Type Promotion

Type promotion occurs when a narrower data type is promoted to the broader data type. For example, int data type can be promoted to long, float, double etc. See the following type promotion table.

Type promotion table:
The data type on the left side can be promoted to the any of the data type present in the right side of it.

byte  short  int  long
short  int  long
int  long  float  double
float  double
long  float  double

 

Example 4

This example shows that one data type is promoted to another data type implicitly if no matching data type is found. See the program code below.

TypePromotionDemo.java

// Example of type promotion

public class TypePromotionDemo {

void add(int a, float b) { //1
System.out.println("Result1: " + (a+b));
}

void add(int a, int b, long c) { //2
System.out.println("Result2: " + (a+b+c));
}

public static void main(String[] args) {
TypePromotionDemo obj = new TypePromotionDemo();
obj.add(25, 15); //1
obj.add(30, 20, 10); //2
}
}
Output:
Result1: 40.0
Result2: 60

 

 

Java Overloaded Constructors

Like methods, constructors can also be overloaded. Since the constructors in a class all have the same name as the class, their signatures are differentiated by their parameter lists. The example below shows that the Cube constructor is overloaded one being the default constructor and the other being a parameterized constructor. 

Example 5

It is possible to use this() construct, to implement local chaining of constructors in a class. The this() call in a constructor invokes the other constructor with the corresponding parameter list within the same class.

For calling the default constructor to create a Cube object results in the second and third parameterized constructors being called as well. Java requires that any this() call must occur as the first statement in a constructor. Below is an example of a cube class containing 3 constructors which demonstrates the this() call in Constructor context.

Cube.java

// Constructor Overloading

public class Cube {
// instance variables
int length;
int breadth;
int height;

Cube() { // Constructor 1
this(10, 10);
System.out.println("Default Constructor");
}

Cube(int l, int b) { // Constructor 2
this(l, b, 10);
System.out.println("Parameterized Constructor with 2 parameters");
}

Cube(int l, int b, int h) { // Constructor 3
length = l;
breadth = b;
height = h;
System.out.println("Parameterized Constructor with 3 parameters");
}

public int getVolume() { // instance method
return (length * breadth * height);
}

public static void main(String[] args) {
Cube cube1, cube2;
cube1 = new Cube();
cube2 = new Cube(10, 20, 30);
System.out.println("Volume of cube1 is : " + cube1.getVolume());
System.out.println("Volume of cube2 is : " + cube2.getVolume());
}

}

Output:

Parameterized Constructor with 3 parameters
Parameterized Constructor with 2 parameters
Default Constructor
Parameterized Constructor with 3 parameters
Volume of cube1 is : 1000
Volume of cube2 is : 6000

 

Overloading of Static Methods

We can have two ore more static methods with same name, but differences in their parameter lists. For example, being static, the main() method can also be overloaded. See the following example.

Example 6

In the given example, main() method is overloaded. 

OverMain.java

//Overload main() method
public class OverMain {

public static void main(String[] args) {
System.out.println("main() method String version called");
main(10);
}

public static void main(int a) {
System.out.println("main() method int version called");
}
}

Output:

main() method String version called
main() method int version called