Java class Constructors Overloading

Introduction

We can overload constructor methods.

The following code overloads the LegoBlock constructor and it initialize the class in two ways.

//LegoBlock defines three constructors to initialize.
class LegoBlock {
  double width;/* www  . ja  v  a2  s.  c  om*/
  double height;
  double depth;

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

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

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

  // compute and return volume
  double volume() {
    return width * height * depth;
  }
}
  
public class Main {
  public static void main(String args[]) {
    // create boxes using the various constructors
    LegoBlock myLegoBlock1 = new LegoBlock(10, 20, 15);
    LegoBlock myLegoBlock2 = new LegoBlock();
    LegoBlock mycube = new LegoBlock(7);

    double vol;

    // get volume of first box
    vol = myLegoBlock1.volume();
    System.out.println("Volume of myLegoBlock1 is " + vol);

    // get volume of second box
    vol = myLegoBlock2.volume();
    System.out.println("Volume of myLegoBlock2 is " + vol);

    // get volume of cube
    vol = mycube.volume();
    System.out.println("Volume of mycube is " + vol);
  }
}

The proper overloaded constructor is called based upon the parameters specified when new is executed.

We can create a new object based on another object.

To do this, we can define a constructor that takes an object as a parameter.


// LegoBlock allows one object to initialize another.
class LegoBlock {
  double width;//from   w  w w .  ja v a2  s .c  o m
  double height;
  double depth;

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

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

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

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

  // compute and return volume
  double volume() {
    return width * height * depth;
  }
}
  
public class Main {
  public static void main(String args[]) {
    // create boxes using the various constructors
    LegoBlock myLegoBlock1 = new LegoBlock(10, 20, 15);
    LegoBlock myLegoBlock2 = new LegoBlock();
    LegoBlock mycube = new LegoBlock(7);

    LegoBlock myclone = new LegoBlock(myLegoBlock1);

    double vol;

    // get volume of first box
    vol = myLegoBlock1.volume();
    System.out.println("Volume of myLegoBlock1 is " + vol);

    // get volume of second box
    vol = myLegoBlock2.volume();
    System.out.println("Volume of myLegoBlock2 is " + vol);

    // get volume of cube
    vol = mycube.volume();
    System.out.println("Volume of cube is " + vol);

    // get volume of clone
    vol = myclone.volume();
    System.out.println("Volume of clone is " + vol);
  }
}



PreviousNext

Related