Site icon DataFlair

Constructor Overloading in Java with the Example

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

After method overloading, here we come up with the concept of Constructor Overloading in Java. It is the process of declaring the same constructor with various parameters in the same class. Here, we will also discuss the copy constructor in Java with example.

Constructor Overloading in Java

Constructor overloading is possible in Java; it is called upon the parameters being executed. In general words, we can say it is a concept of having various constructors with a different parameter list; in this way every constructor performs a different task.

When do we need Constructor Overloading in Java?

We need to initialize the object in different ways as per the requirement, for which we use Java constructor overloading. If we do not specify anything about a thread, then we can use the default, but if we do want to specify, then we use this syntax.

Syntax of Constructor Overloading in Java –

Thread t= new Thread (" MyThread ");

Example of Java Constructor Overloading –

class Box
{
double width, height,depth;
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
double volume()
{
return width * height * depth;
}

The Box() constructor requires three parameters to pass through, and if we do not declare them, then there will be an error. The problem is overcome using constructor overloading.

package com.dataflair.copyconstructor;
class Box
{
  double width, height, length;
  Box(double widthvar, double heightvar, double lengthvar)
  {
    width = widthvar;
    height = heightvar;
    length = lengthvar;
  }
  Box()
  {
    width = height = length = 0;
  }
  Box(double num) 
  { 
    width = height = length = num; 
  } 
  double volume()
  {
    return width * height * length;
  }
}
public class VolumeProgram
{

  public static void main(String args[])
  {
    Box mybox1 = new Box(10,20,30);
    Box mybox2 = new Box(); 
    Box mycube = new Box(5); 

    double volumeOfBox;
    volumeOfBox = mybox1.volume();
    System.out.println(" Value of First Constructor is " + volumeOfBox);

    volumeOfBox = mybox2.volume(); 
    System.out.println(" Value of Second Constructor is " + volumeOfBox); 

    volumeOfBox = mycube.volume(); 
    System.out.println(" Value of Third Constructor is " + volumeOfBox); 	}

}

Output-

Using this() method in Constructor Overloading

this() statement inside the constructor is used during constructor overloading.

package com.dataflair.copyconstructor;
class Box
{
  double width, height, length;
  int boxNo;
  Box(double widthvar, double heightvar, double lengthvar, int num)
  {
    width = widthvar;
    height = heightvar;
    length = lengthvar;
    boxNo = num;
    
  }
  Box()
  {
    width = height = length = 0;
  }
  Box(int num)
  {
  this();
  boxNo = num;
  }
  double volume()
  {
    return width * height * length;
  }
}
public class VolumeProgram
{

  public static void main(String args[])
  {
    Box mybox1 = new Box(10);
    double volumeOfBox;
    volumeOfBox = mybox1.volume();
    System.out.println(" Volume of mybox1 is " + volumeOfBox);
  }

}

Output-

Java Copy Constructor

A Java copy constructor is just like the constructor copying in C++, with only one difference it has to be declared, and not like in C++, it declares itself. It is a special type of constructor that takes the same class as an argument.

Java Copy Constructor Example–

package com.dataflair.copyconstructor;
class ComplexNumber
{
  private double realNumber, imaginaryNumber;
  public ComplexNumber(double realNumber, double imaginaryNumber) 
  {
    this.realNumber = realNumber;
    this.imaginaryNumber = imaginaryNumber;
  }
  ComplexNumber(ComplexNumber copy) 
  {
    System.out.println("Copy constructor called");
    realNumber = copy.realNumber;
    imaginaryNumber = copy.imaginaryNumber;
  }
  @Override
  public String toString() 
  {
    return "(" + realNumber + " + " + imaginaryNumber + "i)";
  }
}
public class CopyConstructorExample 
{

  public static void main(String[] args) {
    ComplexNumber ComplexNumberObject1 = new ComplexNumber(5, 10);
    ComplexNumber ComplexNumberObject2 = new ComplexNumber(ComplexNumberObject1);
    ComplexNumber ComplexNumberObject3 = ComplexNumberObject2;
    System.out.println(ComplexNumberObject2);
  }
}

Output-

Benefits of Constructor Overloading in Java

Constructor overloading offers several advantages in object-oriented programming:

Disadvantages of constructor overloading

Cautions When Using Constructor Overloading

While constructor overloading is a powerful tool, it’s essential to use it judiciously. Here are some factors to consider:

Summary

With the constructor overloading, every constructor can perform a different task. By the end of this article, we learned the importance and how to use this() method in constructor overloading. In addition, we discussed a copy constructor with an example. Hope you like the explanation.

Exit mobile version