Tuesday, 29 August 2017

How to create class abstract figure in java program

WAP that create class abstract figure.

On that class area method is there.

Inherits class rectangle, circle.

All are extend with figure & write the method in all class that prints the area.

Program

import java.util.Scanner;
abstract class figure{
          abstract void area();
}

class circle extends figure{
 Scanner in = new Scanner(System.in);
 int r;

circle(){
          System.out.println("Enter to circle r");
          r = in.nextInt();
}

void area(){
          double ans;
          ans = (3.14 * r * r);
          System.out.println("circle area is" + ans);
          }
}
class rect extends figure{
          Scanner in = new Scanner(System.in);
          float l,w;
          rect()
          {
                             System.out.println("Enter to rect l");
                             l = in.nextInt();
                             System.out.println("Enter to rect w");
                             w = in.nextInt();
          }
          void area(){
          double ans;
          ans = (l*w);
          System.out.println("rect area is" + ans);
          }

}
class call{
          public static void main(String args[])
          {
                   figure c = new circle();
                   c.area();
                   figure r = new rect();
                   r.area();
                  
          }

}

Output

abstract class

0 comments:

Post a Comment