Parameterized Constructors Java Help

Parameter Constructors

While the Box( ) constructor in the preceding example does initialize a Box object, it is . not very useful=-all boxes have the same dimensions. What is needed is a way to
construct Box objects of various dimensions. The easy solution is to add parameters to the constructor. As you can this makes them much more useful. For . example, the following version of Box defines a parameter constructor which sets the dimensions of a box as specified by those parameters. Pay special attention to how Box objects are created.

The output from this program is shown here:
Volume is 3000.0
Volume is 162.0

As you can see, each object is initialized as defined in the parameters to its constructor. For example, in the following line.

the values 10,20, and 15 are passed to the Box( ) constructor when new creates the
object. Thus, my boxl’s copy of width, height, and depth will. contain the values 10, 20, and 15, respectively.

JavaHelpOnline.com

  • Feel free to send us an inquiry, we will reply back in hours.
 

Verification

Posted on September 17, 2014 in Introducing Classes

Share the Story

About the Author

Back to Top