info@meritcampus.com    +91-85006-22255
...
`

Calculate Areas
`class CalculateAreas{    public static void main(String arg[])    {        Rectangle rect = new Rectangle(5.25, 4.0);        System.out.println("Area of rectangle is " + rect.getArea());                Circle circle = new Circle(7.5);        System.out.println("Area of circle is " + circle.getArea());        }}abstract class Shape{    abstract double getArea(); // LINE A}class Rectangle extends Shape{    double length;    double breadth;    Rectangle(double length, double breadth)    {        this.length = length;        this.breadth = breadth;    }    double getArea()    {        return length * breadth;    }}class Circle extends Shape{    double radius;    Circle(double radius)    {        this.radius = radius;    }    double getArea()    {        return 3.14 * radius * radius;    }}`
OUTPUT

Area of rectangle is 21.0
Area of circle is 176.625

DESCRIPTION

Here we have created an abstract `Shape` class and two concrete classes namely `Rectangle` and `Circle` which extends from the `Shape` class. We have implemented the `getArea()` method in the sub-classes. Please note that every sub-class extending from an `abstract` class should implement all the `abstract` methods, otherwise it will cause compilation errors.

THINGS TO TRY
• Define one more sub-class `Triangle` which extends from `Shape` class. Create a constructor, which takes the parameters `base` and `height`. Also implement the `getArea()` method. Area of triangle is (base * height) / 2.0
• Add one more abstract method `getPerimeter()` in the `Shape` class and implement that method in `Rectangle` and `Circle`.
• Define one more sub-class `Polygon` which extends from `Shape`. Do not define any constructors or methods in that class including the `getArea()` method. Observe the compilation error you get.