13.2 Abstract Classes


13.2 Abstract Classes

The inheritance relationship was discussed in Chapter 12. Figure 13.1 illustrates a simple inheritance relationship with four classes Gfigures, Triangle, Circle, and Rectangle.

click to expand
Figure 13.1: A generic base class.

The characteristics of the geometric figures are related in some way. Assume that the relevant attributes defined in these classes are height, base, and radius. The relevant functions defined in the classes are area and perimeter.

An abstract class is one that includes one or more abstract methods. An abstract method has no implementation, only its declaration (also called its specification).

Caution

An abstract class cannot be instantiated; it is generally used as a base class. The subclasses override the abstract methods inherited from the abstract base class.

13.2.1 Defining an Abstract Class

To define an abstract class, the keyword abstract should be used before the keyword class. Every function that does not include its implementation is also preceded by the keyword abstract. An abstract class definition in KJP has the following general structure:

     description            .  .  .     abstract class  class_name  is        private           // private attributes          constants              .  .  .          variables              .  .  .          objects              .  .  .          // private operations               .  .  .         public             // public operations                .  .  .       endclass  class_name  

The attributes of derived classes Rectangle, Triangle, and Circle are different. Classes Rectangle and Triangle have the attributes height and base; Class Circle has the attribute radius. The calculations of area and perimeter are different in each of these subclasses. In this situation, the base class (Gfigures) cannot include the implementations for functions area and perimeter. It can only provide the prototypes for these functions.

A base class is an abstract class when it does not provide the implementation for one or more functions. Such base classes provide single general descriptions for the common functionality and structure of its subclasses. An abstract class is a foundation on which to define subclasses. Classes that are not abstract classes are known as concrete classes.

The base class Gfigures is an abstract class because it does not provide the implementation of the functions area and perimeter. The KJP code with the definition of class Gfigures follows.

       description          This abstract class has two functions. */       abstract class Gfigures is         public         description           This function computes and returns the area           of the geometric figure.   */         abstract function area of type double         description           This function computes and returns the perimeter           of the geometric figure.   */         abstract function perimeter of type double       endclass Gfigures 

On the CD

The KJP code with the implementation of this class is stored in the file Gfigures.kpl. The Java code for class Gfigures is stored in the file Gfigures.java.

Caution

Because the abstract class Gfigures does not include the body of the functions area and perimeter, objects of this class cannot be created. In other words, class Gfigures cannot be instantiated.

In Java, the structure of the abstract class is very similar. The definition of this abstract class follows.

       // KJP v 1.1 File: Gfigures.java, Sat Jan 04                                             19:03:07 2003       /**          This abstract class with two functions.    */       public abstract class Gfigures {         /**          This function computes and returns the area of          the geometric figure.   */         abstract public double area();         /**          This function computes and returns the perimeter          of the geometric figure.   */         abstract public double  perimeter();        }  // end class Gfigures 

13.2.2 Using Abstract Classes

The subclasses that inherit an abstract base class need to override (redefine) the functions that are defined as abstract functions in the abstract base class. The subclasses that are shown in Figure 13.1, Rectangle, Triangle, and Circle, inherit class Gfigures and each includes the specific implementation of the functions area and perimeter and the relevant attributes.

On the CD

The following KJP code includes the complete definition of class Triangle. This inherits the function from class Gfigures and redefines functions area and perimeter. The code for this class is stored in the file Triangle.kpl.

       description             This class computes the area and perimeter             of a triangle, given its three sides.  */       class Triangle inherits Gfigures is         private         variables             double x    // first side             double y    // second side             double z    // third side         public         description             This constructor sets values for the three             sides of the triangle.      */         function initializer parameters double i_x,                               double i_y, double i_z is           begin             set x = i_x             set y = i_y             set z = i_z         endfun initializer         description           This function computes the perimeter of a           triangle. */         function perimeter of type double is           variables              double lperim           begin              set lperim = x + y + z              return lperim         endfun perim         description            This function computes the area of a            triangle.     */         function area of type double is           variables              double s     // intermediate result              double r              double area           begin              set s = 0.5 * (x + y + z)              set r = s * (s - x)*(s - y)*(s - z)              set area = Math.sqrt(r)              return area         endfun area       endclass Triangle 

To construct a complete application with class Triangle, a new class named Mtriangle is defined. This class includes function main that reads the three values for the sides of the triangle. Then, it creates an object of class Triangle and invokes functions area and perimeter of the object.

       description         This program computes the area and peri-         meter of a triangle, given its three sides. */       class Mtriangle is         public         description             This function gets the area and perimeter             of a triangle object.     */         function main is           variables              double x    // first side              double y    // second side              double z    // third side              double area              double perim           objects              object my_triangle of class Triangle           begin             display "This program computes the area"             display " of a triangle"             display "enter value of first side: "             read x             display "enter value of second side: "             read y             display "enter value of third side: "             read z             create my_triangle of class Triangle using x,                                                     y, z             set area = call area of my_triangle             display "Area of triangle is: ", area             set perim = call perimeter of my_triangle             display "Perimeter of triangle is: ", perim          endfun main       endclass Mtriangle 

After translating the classes Triangle and Mtriangle and compiling them with the Java compiler, class Mtriangle can be executed. The output produced on the console is:

       This program computes the area       of a triangle       enter value of first side:       2       enter value of second side:       4       enter value of third side:       5       Area of triangle is: 3.799671038392666       Perimeter of triangle is: 11.0 




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net