July 22, 2022

Constructor Overloading in Java

Constructor overloading in Java is the process similar to method overloading which means with in a class you can have more than one constructor where the constructors differ in the number of parameters.

What is the use of having overloaded constructor

Constructor overloading in Java brings the flexibility of initializing the object as per your need. For example in ArrayList class there is a no-arg constructor which initializes the ArrayList with the default capacity and there is another constructor where initial capacity can be passed as a parameter to initialize ArrayList with the required initial capacity.

Another scenario where constructor overloading in Java helps is to provide a no-arg constructor when there is already a parameterized constructor.

Java implicitly inserts a default no-arg constructor if no constructor is provided in the class. If you provide a constructor yourself then this default no-arg constructor is not provided automatically.

public class ConstructorDemo {
  int num;
  String name;
  // Parameterized constructor
  private ConstructorDemo(int num, String name){
    this.num = num;
    this.name = name;
  }

  public static void main(String[] args) {
    ConstructorDemo cd = new ConstructorDemo();// Compiler error
  }
}

In this class there is a parameterized constructor so default no-arg constructor is not provided automatically by Java. Thus the statement ConstructorDemo cd = new ConstructorDemo(); results in compile time error “The constructor ConstructorDemo() is undefined

In such scenario you can provide more than one constructor to make your object initialization flexible as per your requirement. Like in the above example you can also have a default no-arg constructor along with the parameterized constructor.

public class ConstructorDemo {
  int num;
  String name;
  // Parameterized constructor
  private ConstructorDemo(int num, String name){
    this.num = num;
    this.name = name;
  }
	
  // no-arg constructor
  private ConstructorDemo(){
    
  }

  public static void main(String[] args) {
    ConstructorDemo cd = new ConstructorDemo();
  }
}

Constructor overloading example

Here is an example of constructor overloading where we have an array of type Object. The array is created with the default capacity in the no-arg constructor if no initial capacity is passed. In case initial capacity is passed constructor with initial capacity as parameter is invoked to create an array with passed initial capacity.

public class MyList {
  Object[] elementArray;
  private static final int DEFAULT_CAPACITY = 10;

  public MyList(int initialCapacity) {
    this.elementArray = new Object[initialCapacity];
  }
  public MyList(){
    this.elementArray = new Object[DEFAULT_CAPACITY];
  }
  public static void main(String[] args) {
    MyList myList1 = new MyList();
    System.out.println("Array Length- " + myList1.elementArray.length);
    
    MyList myList2 = new MyList(20);
    System.out.println("Array Length- " + myList2.elementArray.length);
  }
}
Output
Array Length- 10
Array Length- 20

Constructor overloading in Java best practices

In case of having multiple constructors you should be doing the task of initialization in one of the constructor. All the other constructors should ultimately call that constructor for initialization using this keyword.

An example will help to clear it. Consider the scenario where you have 3 fields in your class and you want to give option to initialize all of them or two of them or only a single one or none.

public class ConstrChaining {
  int a;
  double b;
  String name;
  ConstrChaining(){
    this(0);
  }
  ConstrChaining(int a){
    this(a, 0.0);
  }
  ConstrChaining(int a, double b){
    this(a, b, null);
  }
  ConstrChaining(int a, double b, String name){
    this.a = a;
    this.b = b;
    this.name = name;
  }
  public static void main(String[] args) {
    ConstrChaining cc = new ConstrChaining();
    System.out.println("a- " + cc.a + " b- " + cc.b + " name- " + cc.name);
    
    ConstrChaining cc1 = new ConstrChaining(5, 7.8);
    System.out.println("a- " + cc1.a + " b- " + cc1.b + " name- " + cc1.name);
    
    ConstrChaining cc2 = new ConstrChaining(18, 13.45, "knpCode");
    System.out.println("a- " + cc2.a + " b- " + cc2.b + " name- " + cc2.name);
  }
}
Output
a- 0 b- 0.0 name- null
a- 5 b- 7.8 name- null
a- 18 b- 13.45 name- knpCode

In the example you can see that the initialization is done only by the following constructor-

ConstrChaining(int a, double b, String name){
  this.a = a;
  this.b = b;
  this.name = name;
}

All the other overloaded constructors are progressively calling this constructor to get the object initialization done.

That's all for the topic Constructor Overloading in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment