Wednesday, 4 September 2013

constructor in java

class BoxDemo {
  double width;
  double height;
  double depth;
 
  // this is a constructor for Box
    BoxDemo(){
    System.out.println("Constructing BoxDemo");
    width =10;
    height =10;
    depth =10;
    }
    //compute and return value
    double volume(){
    return width*height*depth;
    }
  }

class  Box{
    public static void main(String[] args) {
        BoxDemo mybox = new BoxDemo();
       
        double vol;
        // get the volume of the box
       
        vol = mybox.volume();
       
        System.out.println("VOLUME IS "+vol);
      }
}


// output : Constructing BoxDemo
//             VOLUME IS 1000.0