Previous Page | Next Page

SAS Object-Oriented Programming Concepts

Classes

A class defines a set of data and the operations you can perform on that data. Subclasses are similar to the classes from which they are derived, but they may have different properties or additional behavior. In general, any operation that is valid for a class is also valid for each subclass of that class.


Relationships among Classes

Classes that you define with SCL can support two types of relationships:


Inheritance

Generally, the attributes, methods, events, event handlers, and interfaces that belong to a parent class are automatically inherited by any class that is created from it. One metaphor that is used to describe this relationship is that of the family. Classes that provide the foundation for other classes are called parent classes, and classes that are derived from parent classes are child classes. When more than one class is derived from the same parent class, these classes are related to each other as sibling classes. A descendent of a class has that class as a parent, either directly or indirectly through a series of parent-child relationships. In object-oriented theory, any subclass that is created from a parent class inherits all of the characteristics of the parent class that it is not specifically prohibited from inheriting. The chain of parent classes is called an ancestry.

Class Ancestry

[Class Ancestry]

Whenever you create a new class, that class inherits all of the properties (attributes, methods, events, event handlers, and interfaces) that belong to its parent class. For example, the Object class is the parent of all classes in SAS/AF software. The Frame and Widget classes are subclasses of the Object class, and they inherit all properties of the Object class. Similarly, every class you use in a frame-based application is a descendent of the Frame, Object, or Widget class, and thus inherits all the properties that belong to those classes.


Instantiation

In addition to the inheritance relationship, classes have an instantiation or an "is a" relationship. For example, a frame is an instance of the Frame class; a radio box control is an instance of the Radio Box Control class; and a color list object is an instance of the Color List Model class.

All classes are instances of the Class class. The Class class is a metaclass. A metaclass collects information about other classes and enables you to operate on other classes. For more information about metaclasses, see Metaclasses.


Types of Classes

Some SAS/AF software classes are specific types of classes.


Abstract Classes

Abstract classes group attributes and methods that are common to several subclasses. These classes themselves cannot be instantiated; they simply provide functionality for their subclasses.

The Widget class in SAS/AF software is an example of an abstract class. Its purpose is to collect properties that all widget subclasses can inherit. The Widget class cannot be instantiated.


Models and Views

In SAS/AF software, components that are built on the SAS Component Object Model (SCOM) framework can be classified either as views that display data or as models that provide data. Although models and views are typically used together, they are nevertheless independent components. Their independence allows for customization, flexibility of design, and efficient programming.

Models are non-visual components that provide data. For example, a Data Set List model contains the properties for generating a list of SAS data sets (or tables), given a specific SAS library. A model may be attached to multiple views.

Views are components that provide a visual representation of the data, but they have no knowledge of the actual data they are displaying. The displayed data depends on the state of the model that is connected to the view. A view can be attached to only one model at a time.

It may be helpful to think of model/view components as client/server components. The view acts as the client and the model acts as the server.

For more information on interfaces, see Interfaces. For more information on implementing model/view communication, refer to SAS Guide to Applications Development and to the SAS/AF online Help.


Metaclasses

As previously mentioned, the Class class (sashelp.fsp.Class.class) and any subclasses you create from it are metaclasses. Metaclasses enable you to collect information about other classes and to operate on those classes.

Metaclasses enable you to make changes to the application at run time rather than only at build time. Examples of such changes include where a class's methods reside, the default values of class properties, and even the set of classes and their hierarchy.

Metaclasses also enable you to access information about parent classes, subclasses, and the methods and properties that are defined for a class. Specifically, through methods of the Class class, you can

For more information about metaclasses, see the Class class in the SAS/AF online Help.


Defining Classes

You can create classes in SCL with the CLASS block. The CLASS block begins with the CLASS statement and ends with the ENDCLASS statement:

<ABSTRACT> CLASS class-name<EXTENDS parent-class-name>
<SUPPORTS supports-interface-clause>
<REQUIRES requires-interface-clause>
< / (class-optional-clause)>
<(attribute-statements)>
<(method-declaration-statements)>
<(method-implementation-blocks)>
<(event-declaration-statements)>
<(eventhandler-declaration-statements)>
ENDCLASS;
The CLASS statement enables you to define attributes, methods, events, and event handlers for a class and to specify whether the class supports or requires an interface. The remaining sections in this chapter describe these elements in more detail.

The EXTENDS clause specifies the parent class. If you do not specify an EXTENDS clause, SCL assumes that sashelp.fsp.object.class is the parent class.

Using the CLASS block instead of the Class Editor to create a class enables the compiler to detect errors at compile time, which results in improved performance during run time.

For a complete description of the CLASS statement, see CLASS. For a description of using the Class Editor to define classes, refer to SAS Guide to Applications Development.


Generating CLASS Entries from CLASS Blocks

Suppose you are editing an SCL entry in the Build window and that the entry contains a CLASS block. For example:

class Simple extends myParent;
  public num num1;
  M1: method n:num return=num / (scl='work.a.uSimple.scl');
  M2: method return=num;
    num1 = 3;
    dcl num n = M1(num1);
    return (n);
  endmethod;
endclass;

To generate a CLASS entry from the CLASS block, issue the SAVECLASS command or selectFile [arrow] Save as class...Generating the CLASS entry from the CLASS block is equivalent to using the Class Editor to create a CLASS entry interactively.


Generating CLASS Blocks from CLASS Entries

The CLASS block is especially useful when you need to make many changes to an existing class. To make changes to an existing class, use the CREATESCL function to write the class definition to an SCL entry. You can then edit the SCL entry in the Build window. After you finish entering changes, you can generate the CLASS entry by issuing the SAVECLASS command or selecting File [arrow] Save as class...For more information, see CREATESCL.


Referencing Class Methods or Attributes

Any METHOD block in a class can refer to methods or attributes in its own class without specifying the _SELF_ system variable (which contains the object identifier for the class). For example, if method M1 is defined in class X (and it returns a value), then any method in class X can refer to method M1 as follows:

n=M1();

You do not need to use the _SELF_ system variable:

n=_SELF_.M1();

Omitting references to the _SELF_ variable (which is referred to as shortcut syntax) makes programs easier to read and maintain. However, if you are referencing a method or attribute that is not in the class you are creating, you must specify the object reference.


Instantiating Classes

To instantiate a class, declare a variable of the specific class type, then use the _NEW_ operator. For example:

dcl mylib.classes.collection.class C1;
C1 = _new_ Collection();

You can combine these two operations as follows:

dcl mylib.classes.collection.class C1 = _new_ Collection();

The _NEW_ operator combines the actions of the LOADCLASS function, which loads a class, with the _new method, which initializes the object by invoking the object's _init method.

You can combine the _NEW_ operator with the IMPORT statement, which defines a search path for references to CLASS entries, so that you can refer to these entries with one or two-level names instead of having to use a four-level name in each reference.

For example, you can use the following statements to create a new collection object called C1 as an instance of the collection class that is stored in mylib.classes.collection.class:

   /* Collection class is defined in    */
   /* the catalog MYLIB.MYCAT       */
import mylib.mycat.collection.class;
   /* Create object C1 from a collection class  */
   /* defined in MYLIB.MYCAT.COLLECTION.CLASS */
declare Collection C1=_new_ Collection();

For more information, see _NEW_ and LOADCLASS.

Previous Page | Next Page | Top of Page