Classes, generic functions and object orientation

 The class of an object determines how it will be treated by what are known as generic functions. Put the other way round, a generic function performs a task or action on its arguments specific to the class of the argument itself. If the argument lacks any class attribute, or has a class not catered for specifically by the generic function in question, there is always a default action provided.

An example makes things clearer. The class mechanism offers the user the facility of designing and writing generic functions for special purposes. Among the other generic functions are plot() for displaying objects graphically, summary() for summarising analyses of various types, and anova() for comparing statistical models.

The number of generic functions that can treat a class in a specific way can be quite large. For example, the functions that can accommodate in some fashion objects of class data.frame include \begin{displaymath}
{\tt
\begin{tabular}
{@{\protect\strutt}*{4}{l}}
{[}, & {[[<...
 ...matrix, \\ {[<-}, & model, & plot, & summary, \\ \end{tabular}}\end{displaymath}A currently complete list can be got by using the methods() function:

methods(class="data.frame")

Conversely the number of classes a generic function can handle can also be quite large. For example the plot() function has variants for classes of object \begin{displaymath}
{\tt
\begin{tabular}
{@{\protect\strutt}*{4}{l}}
 data.frame,& default,& density, factor,\\  \end{tabular}}\end{displaymath}and perhaps more. A complete list can be got again by using the methods() function:

methods(plot)

The reader is referred to the official references for a complete discussion of this mechanism.

height 3ex depth 1ex width 0em =1.25truein



Jeff Banfield
2/13/1998