In: Computer Science
Be sure save java file name with Shape2D.java
// creating abstract class
public abstract class Shape2D{
// creating abstract method
public abstract double get2DArea();
}
// creating rectangle2D class that inherits shape2D
class Rectangle2D extends Shape2D{
double length;
double width;
// constructor that takes double inputs
Rectangle2D(double length, double width){
this.length = length;
this.width = width;
}
// returns the rectangle length
public double get2DArea(){
return length*width;
}
}
// creating the circle2D class that inherits Shape2D
class Circle2D extends Shape2D{
double radius;
// constructor that takes double input
Circle2D(double radius){
this.radius = radius;
}
// returns circle length
public double get2DArea(){
return Math.PI*radius*radius;
}
}
// Driver class to test and execute
class Shape2DDriver{
// displayname method to display the area of the
objects we created
public static void displayName(Shape2D nShape){
System.out.println("Area :
"+Math.round(nShape.get2DArea()));
}
public static void main(String[] args) {
Shape2D rectangle = new
Rectangle2D(5.5,2.5);
Shape2D circle = new
Circle2D(2.5);
displayName(rectangle);
displayName(circle);
}
}
SCREENSHOTS: