Sunday, October 21, 2012

Java program to illustrate the concept of abstract class


abstract class figure
{float l,b;
 figure(float x,float y)
 {l=x;
  b=y; }
 abstract float area();
}
class rectangle extends figure
{ rectangle(float a,float b)
{ super(a,b); }
float area()
{ return(l*b); }
}
class triangle extends figure
{triangle(float a,float b)
  {   super(a,b);  }
  float area()
 {return(l*b/2); }
}
class figurearea
{ public static void main(String a[])
 {  rectangle r=new rectangle(2.5f,2.5f);
  triangle t=new triangle(1f,2.0f);
  figure f=r;
  System.out.println("area of rectangle="+f.area());
  f=t;
  System.out.println("area of triangle="+f.area()); }
}

Output

0 comments:

Post a Comment