I am trying to understand what is the difference between the following constructors in java in class
Box
{
Box(Box ob)
{
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = 0;
height = 0;
depth = 0;
}
Box(double width, double height, double depth)
{
this.width = width;
this.height = height;
this.depth = depth;
}
}
Cheers everyone
First is the copy constructor which is used to copy the values of one object to another during initialization.
Second and Fourth is a parametrized constructor which contains all the data members of the class. But it is recomended to use the fourth and most of the IDE(all that I know) will auto generate the 4th as it is easier to read and has the same context
Third is a default constructor. Used to set default values. See it does not take any inputs(as parameter)