Thursday, 6 February 2020

Constructor Chaining in java

public class Chaining {
private  double l,b,h;
public Chaining()
{
l=b=h=10;
}
public Chaining(double le, double br, double he)
{
this();
setDimensions(le,br,he);

}
public boolean setDimensions(double le, double br, double he)
{
if(le >= 0 && br>=0 && he>=0)
{
l=le;
b=br;
h=he;
return true;
}
return false;
}

public String toString()
{
String output="Length = "+l+" Breadth = "+b+" Height = "+h;
return output;
}
public static void main(String args[])
{
Chaining ob = new Chaining(-10,2,3);
System.out.println(ob);

}


}

No comments:

Post a Comment