Learn constructor overloading in Java with simple coding example and uses.

Constructor Overloading

In java programming, constructor overloading means, a class can have more than one constructors with different number of parameters or with different data types.

Before moving further, recommend to read below topic.

We have seen a example of a class having multiple constructors in java. In fact, this is the constructor overloading that comes under compile time polymorphism in java oops.

Let’s understand by example, what does it mean that having constructors with different number of parameters or with different data types with same number of arguments.

Constructors having different number of parameters

In below class, these two constructors have different number of parameters.
A(int a, int b){}
A(int a ,int b, double c){}

Constructors having different data types with same number of parameters

Below two constructors have same number of parameters i.e. one, but, with different data types.
A(int a){}
A(double b){}

Given class.

//Constructors overloading example
class A{
	
	//empty constructor
	A(){}
	
	/*constructors with different data types with same number of argument*/
	A(int a){}
	A(double b){}
	
	/*constructors with with different number of arguments*/
	A(int a, int b){}
	A(int a ,int b, double c){}
	
}

Example of Constructor overloading in java

In below program, three Employee class constructors will be overloaded with different number of signatures. When we create different objects with different parameters, then respective matching constructor get called.

/*
 * Program for java constructor overloading
 * */

class Employee {
	private int id;
	private String name;

	/*----------------------------------------
	 * Three overloaded constructors of the class
	 */
	// Empty constructor
	public Employee() {
		this.id = 0;
		this.name = "Not Available";
	}

	// Overloaded constructor with int parameter
	public Employee(int id) {
		this.id = id;
		this.name = "Not Available";
	}

	// Overloaded constructor with a int parameter and a string
	public Employee(int id, String name) {
		this.id = id;
		this.name = name;
	}

	/*----------------------------------------
	 * Display class fields of the class
	 */

	public void display() {

		System.out.println("Employee Info: ");
		System.out.println("ID: " + this.id);
		System.out.println("Name: " + this.name);
	}

}

/*----------------------------------------
 * Test program for constructor overloading in java
 */

public class ConstructorOverloading {

	public static void main(String[] args) {
		// Call empty constructor
		Employee e1 = new Employee();
		e1.display();

		// Call one parameter constructor
		Employee e2 = new Employee(123);
		e2.display();

		// Call two parameters constructor
		Employee e3 = new Employee(123, "John");
		e3.display();

	}

}

Output:
Employee Info:
ID: 0
Name: Not Available

Employee Info:
ID: 123
Name: Not Available
Employee Info:
ID: 123
Name: John

User of Constructor Overloading in Java

You might be wondering what is the use of constructor overloading in programs. Lets understand by with pseudo example.

We want to write a custom sort class to sort an integer array. And, we want to have two options as given below

  • sort array by default in ascending order
  • sort array with given range.

We can solve these problems using constructor overloading. We can send an array to a class during object creation i.e. to constructor of the class. and can overload another constructor that accept an int array and ranges (first index and second index).

See the pseudo java code

// Custom sort
class CSort {

	// This sort in ascending by default
	CSort(int[] arr) {
		System.out.println("Defulat sorting - Ascending");
		// Call your sorting algorithms
	}

	CSort(int[] arr, int fromIndex, int toIndex) {
		System.out.println("Sorting with given range");
		// Call your sorting algorithm
	}

}

public class Sample {

	public static void main(String[] args) {

		int[] arr = { 7, 2, 5, 3, 6 };
		// Create object that calls matching constructors
		CSort s = new CSort(arr);

		// Sort with range
		CSort si = new CSort(arr, 2, 5);

	}

}

NOTE:

1)When an object of a class in java program is created then a constructor is called implicitly as constructor cannot be called explicitly. So, in above constructor overloading program example, respective overloaded constructors will be called at the time of creation of objects of the class Employee i.e. e1, e2 and e3.

2) Also, note that constructor overloading is compile time polymorphism as similar to java method overloading

Related Posts