Rev 42766 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/Classes.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{Classes}\alias{Classes}\alias{__ClassMetaData}\title{Class Definitions}\description{Class definitions are objects that contain the formal definition of aclass of R objects, usually referred to as an S4 class, todistinguish them from the informal S3 classes.This document gives an overview of S4 classes; fordetails of the class representation objects themselves, see\code{class?classRepresentation} (\code{\link{classRepresentation-class}}).}\details{When a class is defined, an object is stored that contains theinformation about that class, including:\describe{\item{Slots:}{The data contained in an object from an S4 class is defined bythe \emph{slots} in the class definition.Each slot in an object is a component of the object;like components (that is, elements) of alist, these may be extracted and set, using thefunction \code{\link{slot}()} or more often the operator\code{"\link{@}"}. However, theydiffer from list components in important ways.First, slots can only be referred to by name, not by position,and there is no partial matching of names as with list elements.All the objects from a particular class have the same set of slotnames; specifically, the slot names that are contained in theclass definition. Each slot in each object always has the sameclass; again, this is defined by the overall class definition.The phrase \dQuote{has the same class} means that the class of the object inthe slot must be the same as the class specified in thedefinition, or some class that extends the one in thedefinition.One class name is special, \code{.Data}. This stands for the\sQuote{data part} of the object. Any class that contains oneof the basic data types in \R{}, has implicitly a corresponding\code{.Data} slot of that type, allowing computations to extractor replace the data part while leaving other slotsunchanged. The \code{.Data} slot also determines the type of theobject; if \code{x} has a \code{.Data} slot, the type of theslot is the type of the object (that is, the value of\code{\link{typeof}(x)}. Otherwise the type of the object is\code{"S4"}. Extending a basic type this way allows objects touse old-style code for the corresponding type as well as S4methods. Any basic type can be used for \code{.Data}, with theexception of a few that do not behave like ordinary objects;namely, \code{"NULL"}, environments, and external pointers.Classes exist for which there are no actual objects, the\emph{virtual} classes, in fact avery important programming tool. They are used to group togetherordinary classes that want to share some programming behavior,without necessarily restricting how the behavior is implemented.Virtual class definitions may if you want includeslots (to provide some common behavior without fully definingthe object---see \link{traceable-class} for an example).A simple and useful form of virtual class is the \emph{classunion}, a virtual class that is defined in a call to\code{\link{setClassUnion}} by listing one ormore of subclasses (classes that extend the class union). Classunions can include as subclasses basic data types (whosedefinition is otherwise sealed).}\item{Superclasses:}{The definition of a class includes the \emph{superclasses} ---the classes that this class extends. Aclass \code{Fancy}, say, extends a class \code{Simple} if anobject from the \code{Fancy} class has all the capabilities ofthe \code{Simple} class (and probably some more as well). Inparticular, and very usefully, any method defined to work for a\code{Simple} object can be applied to a \code{Fancy} object aswell.This relationship isexpressed equivalently by saying that \code{Simple} is a superclass of\code{Fancy}, or that \code{Fancy} is a subclass of\code{Simple}.The direct superclasses of a class are those superclassesexplicitly defined. Direct superclasses can be defined inthree ways. Most commonly, the superclasses are listed in the\code{contains=} argument in the call to \code{\link{setClass}}that creates the subclass. In this case the subclass willcontain all the slots of the superclass, and the relationbetween the class is called \emph{simple}, as it in fact is.Superclasses can also be definedexplicitly by a call to \code{\link{setIs}}; in this case, therelation requires methods to be specified to go from subclass tosuperclass. Thirdly, a class union is a superclass of all themembers of the union. In this case too the relation is simple,but notice that the relation is defined when the superclass iscreated, not when the subclass is created as with the\code{contains=} mechanism.The definition of a superclass will also potentially containits own direct superclasses. These are considered (and shown) assuperclasses at distance 2 from the original class; their directsuperclasses are at distance 3, and so on. All these arelegitimate superclasses for purposes such as method selection.When superclasses are defined by including the names ofsuperclasses in the \code{contains=} argument to\code{\link{setClass}}, an object from the class will have all the slotsdefined for its own class \emph{and} all the slots defined for allits superclasses as well.The information about the relation between a class and aparticular superclass is encoded as an object of class\code{"SClassExtension"} (see \link{SClassExtension-class}). Alist of such objects for the superclasses (and sometimes for thesubclasses) is included in the metadata object defining theclass. If you need to compute with these objects (for example,to compare the distances), call the function\code{\link{extends}} with argument \code{fullInfo=TRUE}.}\item{Objects:}{The objects from a class, typically created by a call to\code{\link{new}} or by assigning another object from the class,are defined by the \emph{prototype} object for the class and byadditional arguments in the call to \code{\link{new}}, which arepassed to a method for that class for the function\code{\link{initialize}}.Each class definition contains a prototype objectfor the class. This must have values for all the slots defined bythe class definition.By default, these are the prototypes of allthe slot classes, if those are not virtual classes. However, thedefinition of the class can specify any valid object for any ofthe slots.There are a number of \sQuote{basic} classes, corresponding to theordinary kinds of data occurring in R. For example,\code{"numeric"} is a class corresponding to numeric vectors.There are also basic classes corresponding to objects in thelanguage, such as \code{"function"} and \code{"call"}, and forspecialized objects, such as \code{"environment"}These classes are predefined and can then be used as slots or assuperclasses for any other class definitions. The prototypes forthe vector classes are vectors of length 0 of the correspondingtype. Notice that basic classes are unusual in that theprototype object is from the class itself.There are also a few basic virtual classes, the most importantbeing \code{"vector"}, grouping together all the vector classes;and \code{"language"}, grouping together all the types of objectsmaking up the R language.}}}\references{The functions in this package emulate the facility for classes andmethods described in \emph{Programming with Data} (John M. Chambers,Springer, 1998). See this book for further details and examples.}\seealso{\code{\link{Methods}},\code{\link{setClass}},\code{\link{is}},\code{\link{as}},\code{\link{new}},\code{\link{slot}}}\keyword{programming}\keyword{classes}\keyword{methods}