Constructor Overloading in Java | Learn the Implementation of Copy Constructor

Free Java courses with 37 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 the various constructor with a different parameter list, in this way every constructor perform a different task.

1. When do we need Constructor Overloading?

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 do want to specify then we use this syntax.

Syntax –

Thread t= new Thread (" MyThread ");

Example of 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 we declare without declaring 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-

Volume-program-in-Java

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-Using-this-keyword

Java Copy Constructor

A Java copy constructor is just like the constructor copying in C++, with only difference it has to be declared and not like in C++ it will declare itself. It is a special type of constructor which 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-Complex-Number-Program

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.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *