Abstract Class Definitions

Sather - A Language Tutorial
Prev | Chapter 5. Abstract Classes and Subtyping | Next

# 5.2. Abstract Class Definitions

The body of an abstract class definition consists of a semicolon separated list of signatures. Each specifies the signature of a method without providing an implementation at that point. The argument names are required for documentation purposes only and are ignored.

    abstract class $SHIPPING_CRATE is
       destination:$LOCATION;
       weight:FLT;
    end; -- abstract class $SHIPPING_CRATE

Due to the rules of subtyping, which will be introduced lateron (See unnamedlink), there is one restriction on the signatures - `SAME` is permitted only for a return type or out arguments in an abstract class signature.

Abstract types can never be created! Unlike concrete classes, they merely specify an interface to an object, not an object itself. All you can do with an abstract type is to declare a variable to be of that type. Such a variable can point to any actual object which is a subtype of that abstract class. How we determine what objects such an abstract variable can point to is the subject of the next section.

Note that we can, of course, provide a create routine in the abstract class

    abstract class $SHIPPING_CRATE is
       create:SAME; ...

However, we can never call this creation routine on a void abstract class i.e. the following is prohibited

    crate: $SHIPPING_CRATE := #$SHIPPING_CRATE; -- ILLEGAL

In fact, all class calls (:: calls) are prohibited on abstract classes

    f:FLT := $SHIPPING_CRATE::weight;  -- ILLEGAL

Since abstract classes do not define objects, and do not contain shared attributes or constants, such calls on the class are not meaningful.

## Example: An abstract employee

`$EMPLOYEE` illustrates an abstract type. `EMPLOYEE` and `MANAGER` are subtypes. Abstract type definitions specify interfaces without implementations.. Below, we will illustrate how the abstract type may be used.

    abstract class $EMPLOYEE is
       -- Definition of an abstract type.  Any concrete class that
       -- subtypes from this abstract class must provide these routines.
       name:STR;
       id:INT;
    end;

This abstract type definition merely states that any employee must have a name and an id.

## More abstract class examples

Here's an example from the standard library. The abstract class `$STR` represents the set of types that have a way to construct a string suitable for output. All of the standard types such as `INT`, `FLT`, `BOOL` and `CPX` know how to do this, so they are subtypes of `$STR`. Attempting to subtype from `$STR` a concrete class that didn't provide a `str` method would cause an error at compile time.

    abstract class $STR is
       -- Ensures that subtypes have a 'str' routine
       str:STR;   -- Return a string form of the object
    end;

In this illegal abstract class, A and B do not conflict because their arguments are concrete and are not the same type. However, because the argument of C is abstract and unrelated it conflicts with both A and B. D does not conflict with A, B or C because it has a different number of parameters.

    abstract class $FOO is
       foo(arg:INT);      -- method A
       foo(arg:BOOL);     -- method B
       foo(arg:$FOO);     -- method C
       foo(a, b:INT);     -- method D
    end;

Prev | Home | Next
Abstract Classes and Subtyping | Up | Subtyping