Java Copy Constructor | Constructor Overloading in Java

1. Objective

In our last tutorial, we had seen Static Binding Vs Dynamic Binding in Java. Here, we are going to study the different operations that can be carried out using constructors in Java. Moreover, we will discuss what is Constructor Overloading in Java with examples and Java Copy Constructor with examples.

So, let’start Constructor Overloading in Java.

Java Copy Constructor | Constructor Overloading in Java

Java Copy Constructor | Constructor Overloading in Java

2. 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.

Do you know How to Establish Java Socket Connection?

Java Copy Constructor Example–

class Complex {
private double re, im;
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
Complex(Complex c) {
System.out.println("Copy constructor called");
re = c.re;
im = c.im;
}
@Override
public String toString() {
return "(" + re + " + " + im + "i)";
}
}
public class Main {
public static void main(String[] args) {
Complex c1 = new Complex(10, 15);
Complex c2 = new Complex(c1);
Complex c3 = c2;
System.out.println(c2);
}
}

Output-
Copy constructor called
(10.0 + 15.0i)

3. Constructor Overloading in Java

Overloaded constructors are possible in Java; it is called upon the parameters being executed.

Let’s revise Method Overloading in Java with Examples

a. When do we need Constructor Overloading in Java?

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

Syntax –

Thread t= new Thread (" MyThread ");

Example of Constructor Overloading in Java–

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 in Java.

Do you Know About Method Overloading vs Overriding in Java

class Box
{
double width, height, depth;
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = height = depth = 0;
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width * height * depth;
}
}
public class Test
{
public static void main(String args[])
{
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}

Output-
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
A volume of mycube is 343.0

b. Using this() in Constructor Overloading in Java

This() should first statement inside the constructor, it uses during Java constructor overloading.

class Box
{
double width, height, depth;
int boxNo;
Box(double w, double h, double d, int num)
{
width = w;
height = h;
depth = d;
boxNo = num;
}
Box()
{
width = height = depth = 0;
}
Box(int num)
{
this();
boxNo = num;
}
public static void main(String[] args)
{
Box box1 = new Box(1);
System.out.println(box1.width);
}
}

Output-
0.0
Let’s Discuss Serialization and Deserialization in Java

This was all about Java Copy Constructor Tutorial. Hope you like our explanation on Constructor Overloading in Java. 

4. Conclusion

Hence, in this Java Constructor Overloading tutorial, we studied about, Java Copy Constructor with examples and Constructor Overloading in Java Programming Language. In conclusion, we learned when do we need and how to use this() in Java Constructor Overloading. Furthermore, if you have any doubts, feel free to ask in the comment section.

See Also- Multithreading in Java 

For reference

Leave a Reply

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