w3resource

Java Polymorphism: Shape base class with Circle and Cylinder subclasses

Java Polymorphism: Exercise-12 with Solution

Write a Java program to create a base class Shape with methods draw() and calculateArea(). Create two subclasses Circle and Cylinder. Override the draw() method in each subclass to draw the respective shape. In addition, override the calculateArea() method in the Cylinder subclass to calculate and return the total surface area of the cylinder.

Sample Solution:

Java Code:

//Shape.java
abstract class Shape {
  public abstract void draw();

  public abstract double calculateArea();
}

//Circle.java
class Circle extends Shape {
  private double radius;

  public Circle(double radius) {
    this.radius = radius;
  }

  @Override
  public void draw() {
    System.out.println("Drawing a circle");
  }

  @Override
  public double calculateArea() {
    return Math.PI * radius * radius;
  }

  protected double getRadius() {
    return radius;
  }
}
//Cylinder.java
class Cylinder extends Circle {
  private double height;

  public Cylinder(double radius, double height) {
    super(radius);
    this.height = height;
  }

  @Override
  public void draw() {
    System.out.println("Drawing a cylinder");
  }

  @Override
  public double calculateArea() {
    // Calculate the total surface area of the cylinder (including the circular top and bottom)
    double circleArea = super.calculateArea();
    double sideArea = 2 * Math.PI * getRadius() * height;
    return 2 * circleArea + sideArea;
  }
}
//Main.java
public class Main {
  public static void main(String[] args) {
    Shape circle = new Circle(7.0);
    Shape cylinder = new Cylinder(4.0, 9.0);

    drawShapeAndCalculateArea(circle);
    drawShapeAndCalculateArea(cylinder);
  }

  public static void drawShapeAndCalculateArea(Shape shape) {
    shape.draw();
    double area = shape.calculateArea();
    System.out.println("Area: " + area);
  }
}

Sample Output:

Drawing a circle
Area: 153.93804002589985
Drawing a cylinder
Area: 326.7256359733385

Explanation:

In the above exercise -

  • The "Shape" class is the base abstract class, and Circle and Cylinder are its concrete subclasses. Each subclass overrides the draw() method to draw the respective shape and the calculateArea() method to calculate and return the area of each shape.
  • In the "Main" class, we have a static method drawShapeAndCalculateArea(Shape shape) that takes an object of the base class Shape as a parameter. Inside this method, we call the draw() and calculateArea() methods on the shape object. Since the drawShapeAndCalculateArea method takes a Shape type parameter, it can accept Circle and Cylinder objects.

Flowchart:

Flowchart: Shape Java
Flowchart: Circle Java
Flowchart: Cylinder Java
Flowchart: Main Java

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Vehicle base class with Car and Motorcycle subclasses.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.