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

COMPUTER SOFTWARE APPLICATION - CITS



           EXERCISE 110 : Create abstract classes and methods


            Objectives

           At the end of this exercise you shall be able to
           •  develop Java programs to Create abstract classes and methods.


           Requirements

           Tools/Materials
           •  PC / laptop with windows OS
           •   SDK software
           •  Test editor (Visual studio/ subline/ notepad)

           Procedure

           TASK 1: Create abstract classes and methods example

              // Abstract class
              abstract class Shape {
                  // Abstract method
                  public abstract double calculateArea();

                  // Concrete method
                  public void displayArea() {
                      System.out.println(“Area: “ + calculateArea());
                  }
              }

              // Concrete class 1
              class Circle extends Shape {
                  private double radius;
                  // Constructor
                  public Circle(double radius) {

                      this.radius = radius;
                  }
                  // Implementation of abstract method
                  @Override
                  public double calculateArea() {

                      return Math.PI * radius * radius;
                  }
              }
              // Concrete class 2










                                                           163
   173   174   175   176   177   178   179   180   181   182   183