Wednesday, November 21, 2012

abstract class in java

abstract class Figure
{
double dim1;
double dim2;
Figure(double a,double b)
{
dim1=a;
dim2=b;
}
abstract double area();
}
class Rectangle extends Figure
{
Rectangle(double a,double b)
{
super(a,b);
}
double area()
{
System.out.println("Area of Rectangle:");
return dim1*dim2;
}
}

class Triangle extends Figure
{
Triangle(double a,double b)
{
super(a,b);
}
double area()
{
System.out.println("Area of Triangle");
return((dim1*dim2)/2);
}
}

class Abs
{
public static void main(String args[])
{

Figure f=new Figure(10,10);
Rectangle r=new Rectangle(10,12);
Triangle t=new Triangle(10,18);
Figure fig;
fig=f;
System.out.println(fig.area());
fig=r;
System.out.println(fig.area());
fig=t;
System.out.println(fig.area());
}
}

0 comments:

Post a Comment