Wednesday, December 23, 2015

Overloading Constructor:

Overloading Constructor:

package overloadingconstrucror;

public class Box {
    double width;
    double height;
    double depth;
   
    Box(double w,double h,double d){
    width=w;
    height=h;
    depth=d;
    }
   
    Box(){
    width=-2;
    height=-2;
    depth=-2;
    }
   
    //construcror used when cube is created
    Box(double leng){
    width=height=depth=leng;
    }
    double volume(){
    return width*height*depth;
    }
}



public class OverloadConstructor {
    public static void main(String[] args) {
        Box obj = new Box(5,10,15);
        Box obj1= new Box();
        Box obj2 = new Box(6);
        
        double vol;
        
        vol=obj.volume();
        System.out.println("volume "+vol);
        
        vol=obj2.volume();
        System.out.println("Volume "+vol);
    }
}

The above code give the output as:
run:
volume 750.0
Volume 216.0

0 comments:

Post a Comment