Page 179 - CTS - CSA TP - Volume 2
P. 179

COMPUTER SOFTWARE APPLICATION - CITS




              class Rectangle extends Shape {
                  private double length;
                  private double width;

                  // Constructor
                  public Rectangle(double length, double width) {
                      this.length = length;
                      this.width = width;

                  }
                  // Implementation of abstract method
                  @Override
                  public double calculateArea() {
                      return length * width;
                  }

              }
              // Main class
              public class AbstractClassExample {
                  public static void main(String[] args) {

                      // Create instances of concrete classes
                      Circle circle = new Circle(5.0);
                      Rectangle rectangle = new Rectangle(4.0, 6.0);


                      // Call abstract and concrete methods

                      circle.displayArea();    // Calls abstract method implementation in Circle
                      rectangle.displayArea(); // Calls abstract method implementation in Rectangle
                  }
              }
           Explanation:

           1  Abstract Class (Shape):
              •  Declares an abstract method calculateArea() without implementation.
              •  Defines a concrete method displayArea() that calls the abstract method.
           2  Concrete Classes (Circle and Rectangle):
              •  Extend the abstract class Shape.

              •  Provide implementations for the abstract method calculateArea().
           3  Main Class (AbstractClassExample):
              •  Creates instances of concrete classes.
              •  Demonstrates calling abstract and concrete methods.










                                                           164
                                CITS : IT & ITES - Computer Software Application - Exercise 110
   174   175   176   177   178   179   180   181   182   183   184