Why do we use constructor overload

Tutorial: Constructor Overload in Java

We have already discussed everything about Java constructors in our previous tutorial and we have also discussed different types of constructors, viz. Standard constructor, parameterized constructor, and copy constructor. Before we get into the details of constructor overloading, let's summarize the constructor one more time.

Recap the construct

  • Constructors are used to assign initial values ​​to instance variables of the class immediately after the object is created.
  • The constructor is always called by the new operator.
  • A default constructor with no arguments is automatically called by the Java Virtual Machine (JVM).
  • Constructors are declared the same way we declare methods, except that the constructor has no return type.

What is constructor overload in Java?

If the constructors have the same name with different arguments (can differ in the number / type / order of the arguments), this is known as constructor overload.

Properties:

  • In a class, the concept of constructor overload does not allow the external user to know about the internal processing of the system.
  • Overloaded constructors differ either in the number of parameters or the type of parameters or the order of the parameters.
  • JVM distinguishes constructors based on the following criteria
    1. Number of parameters.
    2. Type of parameters.
    3. Order of parameters.
  • Overloading constructors allows the user to achieve the compile-time polymorphism.

program

class Figure {double width; double height; double depth; / * Default Constructor * / Figure () {width = -1; height = -1; depth = -1; } / * Parameterized constructor when all the dimensions are specified * / Figure (double width, double height, double depth) {this.width = width; this.height = height; this.depth = depth; } / * Parameterized constructor to calculate volume of Cube * / Figure (double length) {width = height = depth = length; } / * Copy Constructor * / Figure (Figure fig) {width = fig.width; height = fig.height; depth = fig.depth; } / * Method to calculate Volume * / double volume () {return width * height * depth; }} class CalculateVolume {public static void main (String args []) {Figure fig = new Figure (); Figure pp = new Figure (2,3,4); Figure cube = new Figure (3); Figure box = new Figure (pp); System.out.println ("Volume of default Figure is:" + fig.volume ()); System.out.println ("Volume of Parallelopiped is:" + pp.volume ()); System.out.println ("Volume of Cube is:" + cube.volume ()); System.out.println ("Volume of Box is:" + box.volume ()); }}

output

Explanation of the Java code and the output

Here in the Figure class, the Figure () constructors are overloaded with various arguments in order to initialize instance variables and to calculate the volume of these geometric figures. In the Figure class we have defined a standard constructor to initialize all instance variables with some standard values. Here it is -1. The next two constructors are parameterized constructors where the first constructor specifies all dimensions and the second specifies a dimension for the cube. The last one is the copy constructor when we need a figure that can be identical to the existing figure. Then we can use this constructor to initialize the dimensions of the figure that we need to create. Therefore, during object creation, the JVM distinguishes all of these overloaded constructors based on the resolution criteria mentioned in the properties above.

Another basic I want to summarize is declared as a parameterized constructor and without declaring an explicit default declaration, if you want to create an object of this class using the default constructor of that class, you will get an error message if you declare the parameterized constructor at this point. JVM does not provide its own default constructor. You must explicitly specify the default constructor while providing the parameterized constructor to avoid instantiation errors.

NB: Automatic data type conversion also applies to constructor overload. This conversion applies to the conversion of a lower data type (e.g. int) to a higher data type (e.g. double), not the inverse (double to int).

Next we will learn about it how to return objects from Java methods.

More useful tutorials and definitive guidelines on Java programming can be found here.