Write A Program In Java Which Has An Abstract Class Shape Having Three Subclasses: Triangle, Rectangle, And Circle. Define Method Area() In The Abstract Class Shape And Override Area() Method To Calculate The Area

0

Write A Program In Java Which Has An Abstract Class Shape Having Three Subclasses: Triangle, Rectangle, And Circle. Define Method Area() In The Abstract Class Shape And Override Area() Method To Calculate The Area.

Write a program in java which has an abstract class shape having three subclasses: triangle, rectangle, and circle. Define method area() in the abstract class shape and override area() method to calculate the area

Here's a program in java that defines an abstract class shape with three subclasses triangle, rectangle, and circle, and implements the area() method in each subclass to calculate the area of the corresponding shape:


Program

abstract class Shape {
    public abstract double area();
}

class Triangle extends Shape {
    private double base;
    private double height;

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

    public double area() {
        return 0.5 * base * height;
    }
}

class Rectangle extends Shape {
    private double length;
    private double width;

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

    public double area() {
        return length * width;
    }
}

class Circle extends Shape {
    private double radius;

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

    public double area() {
        return Math.PI * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        Triangle triangle = new Triangle(5, 10);
        Rectangle rectangle = new Rectangle(5, 10);
        Circle circle = new Circle(5);

        System.out.println("Area of Triangle: " + triangle.area());
        System.out.println("Area of Rectangle: " + rectangle.area());
        System.out.println("Area of Circle: " + circle.area());
    }
}

Output
Area of Triangle: 25.0 Area of Rectangle: 50.0 Area of Circle: 78.53981633974483

Explanation

We first define an abstract class shape with a single abstract method area(). This class serves as the base class for all shapes that we want to define in our program. By making this class abstract, we ensure that it cannot be instantiated directly, but can only be used as a base class for other classes.


We then define three subclasses of shape: triangle, rectangle, and circle. Each subclass implements its own version of the area() method, which calculates the area of the corresponding shape.


The triangle class takes two parameters, base and height, which represent the base and height of the triangle respectively. In the area() method, we simply return half of the product of base and height, which gives us the area of the triangle.


The rectangle class takes two parameters, length and width, which represent the length and width of the rectangle respectively. In the area() method, we simply return the product of length and width, which gives us the area of the rectangle.


The circle class takes one parameter, radius, which represents the radius of the circle. In the area() method, we use the formula for the area of a circle, which is pi * r^2, where r is the radius and pi is the constant value of pi.


Finally, in the main() method, we create one instance of each subclass with appropriate parameters, and call the area() method on each instance to calculate the area of the corresponding shape. We use system.Out.Println() to print the area of each shape to the console


This program demonstrates the use of an abstract base class to define multiple subclasses, each with their own implementation of a common method. It also demonstrates polymorphism, as we can treat all shapes as instances of the shape class, and call the area() method on each instance to calculate the area of the corresponding shape.


❤️ I Hope This Helps You Understand, Click Here To Do More Exercise In Java Programming.


Tags

Post a Comment

0Comments
Post a Comment (0)