w3resource

Java Polymorphism Programming - Shape Class with Circle, Rectangle, and Triangle Subclasses for Area Calculation

Java Polymorphism: Exercise-3 with Solution

Write a Java program to create a base class Shape with a method called calculateArea(). Create three subclasses: Circle, Rectangle, and Triangle. Override the calculateArea() method in each subclass to calculate and return the shape's area.

In the given exercise, here is a simple diagram illustrating polymorphism implementation:

Polymorphism: Shape Class with Circle, Rectangle, and Triangle Subclasses for Area Calculation

In the above diagram, the Shape class is the base class, and it has a single method calculateArea(). The Circle, Rectangle, and Triangle classes inherit from the Shape class and override the calculateArea() method.

This diagram visually represents the class structure and the polymorphism relationship between the classes involved in the implementation.

Sample Solution:

Java Code:

// Shape.java
// Base class Shape
public class Shape {
    public double calculateArea() {
        return 0; // Default implementation returns 0
    }
}

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

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

    @Override
    public double calculateArea() {
        return Math.PI * radius * radius; // Calculate area of circle
    }
}
// Rectangle.java
// Subclass Rectangle
public class Rectangle extends Shape {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double calculateArea() {
        return width * height; // Calculate area of rectangle
    }
}
// Triangle.java
// Subclass Triangle
public class Triangle extends Shape {
    private double base;
    private double height;

    public Triangle(double base, double height) {
        this.base = base;
        this.height = height;
    }

    @Override
    public double calculateArea() {
        return 0.5 * base * height; // Calculate area of triangle
    }
}
// Main.java
// Main class
public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle(4);
        System.out.println("Area of Circle: " + circle.calculateArea());

        Rectangle rectangle = new Rectangle(12, 34);
        System.out.println("\nArea of Rectangle: " + rectangle.calculateArea());

        Triangle triangle = new Triangle(5, 9);
        System.out.println("\nArea of Triangle: " + triangle.calculateArea());
    }
}

Sample Output:

Area of Circle: 50.26548245743669

Area of Rectangle: 408.0

Area of Triangle: 22.5

Flowchart:

Flowchart: Base class Shape
Flowchart: Subclass Circle
Flowchart: Subclass Rectangle
Flowchart: Subclass Triangle
Flowchart: Main class

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Vehicle Class with Car and Bicycle Subclasses for Speed Control.
Next: Employee Class with Manager and Programmer Subclasses for Salary Calculation.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.