Write a Java Program to create an abstract class named Shape that contains two integers andan empty method named printArea(). Provide three classes named Rectangle, Triangle andCircle such that each one of the classes extends the class Shape. Each one of the classescontains only the method printArea() that prints the area of the given shape.

Program:

import java.util.*;
abstract class shape
{
int x,y;
abstract void area(double x,double y);
}
class Rectangle extends shape
{
void area(double x,double y)
{
System.out.println("Area of rectangle is :"+(x*y));
}
}
class Circle extends shape
{
void area(double x,double y)
{
System.out.println("Area of circle is :"+(3.14*x*x));
}
}
class Triangle extends shape
{
void area(double x,double y)
{
System.out.println("Area of triangle is :"+(0.5*x*y));
}
}
class AbstactDDemo
{
public static void main(String[] args)
{
Rectangle r=new Rectangle();
r.area(2,5);
Circle c=new Circle();
c.area(5,5);
Triangle t=new Triangle();
t.area(2,5);
}
}

Output:

Area of rectangle is :10.0
Area of circle is :78.5
Area of triangle is :5.0

Leave a comment