publicclassAbstractAreas{
publicstaticvoidmain(String[] args){
//Figure f = new Figure(10 , 10);//illegal now
Rectangle r = new Rectangle(9 , 5);
Triangle t = new Triangle(10 , 8);
Figure figref;//this is OK , no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}
//Using abstract methods and classes.abstractclassFigure{
double dim1;
double dim2;
Figure(double a , double b)
{
dim1 = a;
dim2 = b;
}
//area is now an abstract methodabstractdoublearea();
}
classRectangleextendsFigure{
Rectangle(double a , double b)
{
super(a , b);
}
//override area for tectangledoublearea(){
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
classTriangleextendsFigure{
Triangle(double a , double b)
{
super(a , b);
}
//override area for right triangledoublearea(){
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 /2;
}
}
/*
result:
Inside Area for Rectangle.
Area is 45.0
Inside Area for Triangle.
Area is 40.0
*/