Welcome to Java Examples

Take a cup of tea and Let's Start programming

Describe abstract class called Shape which has three subclasses say Triangle,Rectangle,Circle. Define one method area() in the abstract class and override this area() in these three subclasses to calculate for specific object i.e.area() of Triangle subclass should calculate area of triangle etc. Same for Rectangle and Circle.

import java.lang.Math;

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

class Triangle extends Shape
{
double b=50,h=15;
void area()
{
area = (b*h)/2;
System.out.println("area of Triangle -->"+area);
}
}

class Rectangle extends Shape
{
double w=70,h=20;
void area()
{
area = w*h;
System.out.println("area of Rectangle -->"+area);
}
}

class Circle extends Shape
{
double r=5;
void area()
{
area = Math.PI * r * r;
System.out.println("area of Circle -->"+area);
}
}

class Area
{
public static void main(String [] args)
{
Triangle t= new Triangle();
Rectangle r =new Rectangle();
Circle c =new Circle();

t.area();
r.area();
c.area();
}
}


output


0 comments :

Post a Comment