In: Computer Science
abstract class Shape2D
{
public abstract double get2DArea();
}
class Rectangle2D extends Shape2D
{
private double length,width;
public Rectangle2D(double length,double width)
{
this.length = length;
this.width = width;
}
public double get2DArea()
{
return length*width;
}
}
class Circle2D extends Shape2D
{
private double radius;
public Circle2D(double radius)
{
this.radius = radius;
}
public double get2DArea()
{
return Math.PI*radius*radius;
}
}
class Shape2DDriver
{
public static void main (String[] args)
{
Rectangle2D r = new Rectangle2D(5.5,6.5);
display(r);
Circle2D c = new Circle2D(4.0);
display(c);
}
public static void display(Shape2D obj)
{
System.out.printf("\narea =
%.1f",obj.get2DArea());
}
}
Output:
area = 35.8 area = 50.3
Do ask if any doubt. Please up-vote.