Problem Statement is "To develop a Java Program to create an abstract class named
Shape
that contains two integers and an empty method namedprintArea()
. Provide three classes namedRectangle
,Triangle
andCircle
such that each one of the classes extends the class Shape. Each one of the classes contains only the methodprintArea()
that prints the area of the given shape."
In this program, I want to get the two integer values contained by the abstract class Shape
, from the user (run time) instead of compile time.
This is my code
abstract class Shape
{
abstract void Printarea();
int a=10,b=2;;
}
class Rectangle extends Shape
{
void Printarea()
{
System.out.println("area of rectangle is "+(a*b));
}
}
class Triangle extends Shape
{
void Printarea()
{
System.out.println("area of triangle is "+(0.5*a*b));
}
}
class Circle extends Shape
{
void Printarea()
{
System.out.println("area of circle is "+(3.14*a*a));
}
}
class Main
{
public static void main(String []args)
{
Shape=b;
b=new Circle();
b.Printarea();
b=new Rectangle();
b.Printarea();
b=new Triangle();
b.Printarea();
}
}