// Abstract class
abstract class Shape
{
double dim1, dim2;
Shape(double a)
{
dim1 = a;
dim2 = 0;
}
Shape(double a, double b)
{
dim1 = a;
dim2 = b;
}
// Abstract method
abstract void printArea();
}
// Rectangle class
class Rectangle extends Shape
{
Rectangle(double length, double breadth)
{
super(length, breadth);
}
void printArea()
{
double area = dim1 * dim2;
System.out.printf("Area of Rectangle = %.2f%n", area);
}
}
// Triangle class
class Triangle extends Shape
{
Triangle(double base, double height)
{
super(base, height);
}
void printArea()
{
double area = 0.5 * dim1 * dim2;
System.out.printf("Area of Triangle = %.2f%n", area);
}
}
// Circle class
class Circle extends Shape
{
Circle(double radius)
{
super(radius);
}
void printArea()
{
double area = Math.PI * dim1 * dim1;
System.out.printf("Area of Circle = %.2f%n", area);
}
}
public class Demo_Abstract
{
public static void main(String as[])
{
Rectangle r = new Rectangle(10, 5);
r.printArea();
Triangle t = new Triangle(6, 8);
t.printArea();
Circle c = new Circle(7);
c.printArea();
}
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu