Java Source File
Download
abstract class Shape {
String color;
Shape(String color) {
this.color = color;
}
abstract double calculateArea();
void displayColor() {
System.out.println("Color: " + color);
}
}
class Circle extends Shape {
double radius;
Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
double width, height;
Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}
@Override
double calculateArea() {
return width * height;
}
}
public class Main {
public static void main(String[] args) {
// Shape shape1 = new Shape("Green");//ERROR! cannot instantiate abstract class
Circle circle = new Circle("Red", 5);
Rectangle rectangle = new Rectangle("Blue", 4, 6);
System.out.println("Circle Area: " + circle.calculateArea());
circle.displayColor();
System.out.println("Rectangle Area: " + rectangle.calculateArea());
rectangle.displayColor();
}
}