Q) Simple Inheritance
Explain the above using GeometricShape Class with attributes borderColor(String), filled (Boolean type). This is inherited by Rectangle Class with length and width as attributes. Add mutators, accessors and toString() methods
Multilevel Inheritance
Enhance the above design where Cuboid class with height field inherits the Rectangle class.
Hierarchy Inheritance
Enhance the design of Simple Inheritance where Circle class with radius field also inherits from GeometricShape.
class GeometricShapeClass
{
private String bColor;
private boolean filled;
public GeometricShapeClass(String c, boolean f)
{
bColor=c;
filled=f;
}
public String toString()
{
return " Border color = "+bColor+" filled = "+filled;
}
}
class Rectangle extends GeometricShapeClass
{
double l,w;
public Rectangle(String c, boolean f,double le, double wi)
{
super(c,f);
l=le;
w=wi;
}
public String toString()
{
return super.toString()+ " length = " +l+ " width = " + w;
}
}
class Cuboid extends Rectangle
{
double h;
public Cuboid(String c, boolean f,double le, double wi, double he)
{
super(c,f,le,wi);
h=he;
}
public String toString()
{
return super.toString()+" height = "+h;
}
}
class Circle extends GeometricShapeClass
{
double radius;
public Circle(String c, boolean f, double r)
{
super(c,f);
radius=r;
}
public String toString()
{
return super.toString()+ " radius = "+radius;
}
}
public class Demo
{
public static void main(String args[])
{
Cuboid ob1 = new Cuboid("black",true,10,30,60);
System.out.println(ob1);
Circle ob2 = new Circle("red",false,20.5);
System.out.println(ob2);
}
}
Explain the above using GeometricShape Class with attributes borderColor(String), filled (Boolean type). This is inherited by Rectangle Class with length and width as attributes. Add mutators, accessors and toString() methods
Multilevel Inheritance
Enhance the above design where Cuboid class with height field inherits the Rectangle class.
Hierarchy Inheritance
Enhance the design of Simple Inheritance where Circle class with radius field also inherits from GeometricShape.
class GeometricShapeClass
{
private String bColor;
private boolean filled;
public GeometricShapeClass(String c, boolean f)
{
bColor=c;
filled=f;
}
public String toString()
{
return " Border color = "+bColor+" filled = "+filled;
}
}
class Rectangle extends GeometricShapeClass
{
double l,w;
public Rectangle(String c, boolean f,double le, double wi)
{
super(c,f);
l=le;
w=wi;
}
public String toString()
{
return super.toString()+ " length = " +l+ " width = " + w;
}
}
class Cuboid extends Rectangle
{
double h;
public Cuboid(String c, boolean f,double le, double wi, double he)
{
super(c,f,le,wi);
h=he;
}
public String toString()
{
return super.toString()+" height = "+h;
}
}
class Circle extends GeometricShapeClass
{
double radius;
public Circle(String c, boolean f, double r)
{
super(c,f);
radius=r;
}
public String toString()
{
return super.toString()+ " radius = "+radius;
}
}
public class Demo
{
public static void main(String args[])
{
Cuboid ob1 = new Cuboid("black",true,10,30,60);
System.out.println(ob1);
Circle ob2 = new Circle("red",false,20.5);
System.out.println(ob2);
}
}
No comments:
Post a Comment