1
class Box {
private double width;
private double height;
private double depth;

// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}

// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}

// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}

// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}

// compute and return volume
double volume() {
return width * height * depth;
   }
}

// BoxWeight now fully implements all constructors.
class BoxWeight extends Box {
double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob) { // pass object to constructor
super(ob);
weight = ob.weight;
}

// constructor when all parameters are specified
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}

// default constructor
BoxWeight() {
super();
weight = -1;
 }

// constructor used when cube is created
BoxWeight(double len, double m) {
super(len);
weight = m;
   }
 }

class DemoSuper {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
BoxWeight mybox3 = new BoxWeight(); // default
BoxWeight mycube = new BoxWeight(3, 2);
BoxWeight myclone = new BoxWeight(mybox1);

double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
vol = mybox3.volume();
System.out.println("Volume of mybox3 is " + vol);
System.out.println("Weight of mybox3 is " + mybox3.weight);
System.out.println();
vol = myclone.volume();
System.out.println("Volume of myclone is " + vol);
System.out.println("Weight of myclone is " + myclone.weight);
System.out.println();
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
System.out.println("Weight of mycube is " + mycube.weight);
System.out.println();
   }
 }

In the above code can someone please explain me what is the use of the following constructor Box(Box ob) and BoxWeight(BoxWeight ob)

I mean why we are creating Box and BoxWeight object 'ob'and passing it as a parameter in the constructor.

I am just asking what is the use of copy constructors . I have read its definitions but I didn't understood . So please help me in understood this in easy words. If you have any easy examples to let me understood , it would be appreciated.

nitin
  • 31
  • 6
  • The answer is in the question: *construct clone of an object*. That said, if that's really the whole code of the classes, the constructor is useless, since the classes are effectively immutable (or at least, Box is. BoxWheight would be if the weight field was private). So constructing a clone is useless: you can safely pass the original object without cloging it, since noone can modify its state. – JB Nizet Aug 15 '17 at 08:35
  • It's called a "copy constructor" - a constructor that creates a copy. – S.L. Barth is on codidact.com Aug 15 '17 at 08:36
  • 1
    *Please* format your code when you post. It's very *very* hard to read without any indentation. – Jon Skeet Aug 15 '17 at 08:36
  • 1
    Please read [ask], and **do not ask people not to down-vote** your question. Make sure you post a good question. It doesn't matter if you are a beginner or not. What matters is the quality of the question. – RealSkeptic Aug 15 '17 at 08:37
  • I didn't found my answer in your referred question. Please help me. – nitin Aug 18 '17 at 13:03

0 Answers0