\HeaderA{class}{Object Classes}{class}
\aliasA{class<\Rdash}{class}{class<.Rdash.}
\aliasA{inherits}{class}{inherits}
\aliasA{oldClass}{class}{oldClass}
\aliasA{oldClass<\Rdash}{class}{oldClass<.Rdash.}
\aliasA{unclass}{class}{unclass}
\keyword{methods}{class}
\keyword{classes}{class}
\begin{Description}\relax
\R{} possesses a simple generic function mechanism which can be used for
an object-oriented style of programming.  Method dispatch takes place
based on the class of the first argument to the generic function.
\end{Description}
\begin{Usage}
\begin{verbatim}
class(x)
class(x) <- value
unclass(x)
inherits(x, what, which = FALSE)

oldClass(x)
oldClass(x) <- value
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] a \R{} object
\item[\code{what, value}] a character vector naming classes.
\item[\code{which}] logical affecting return value: see Details.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
Many \R{} objects have a \code{class} attribute, a character vector
giving the names of the classes which the object \dQuote{inherits}
from.  If the object does not have a class attribute, it has an
implicit class, \code{"matrix"}, \code{"array"} or the result of
\code{\LinkA{mode}{mode}(x)}.  (Functions \code{oldClass} and
\code{oldClass<-} get and set the attribute, which can also be done
directly.)

When a generic
function \code{fun} is applied to an object with class attribute
\code{c("first", "second")}, the system searches for a function called
\code{fun.first} and, if it finds it, applies it to the object.  If no
such function is found, a function called \code{fun.second} is tried.
If no class name produces a suitable function, the function
\code{fun.default} is used (if it exists).
If there is no class attribute, the implicit class is tried, then the
default method.

The function \code{class} prints the vector of names of classes an
object inherits from.  Correspondingly, \code{class<-} sets the
classes an object inherits from.

\code{unclass} returns (a copy of) its argument with its class
attribute removed.  (It is not allowed for objects which cannot be
copied, namely environments and external pointers.)

\code{inherits} indicates whether its first argument inherits from any
of the classes specified in the \code{what} argument.  If \code{which}
is \code{TRUE} then an integer vector of the same length as
\code{what} is returned. Each element indicates the position in the
\code{class(x)} matched by the element of \code{what}; zero indicates
no match. If \code{which} is \code{FALSE} then \code{TRUE} is 
returned by \code{inherits} if any of the names in \code{what} match
with any \code{class}.
\end{Details}
\begin{Section}{Formal classes}
An additional mechanism of \emph{formal} classes is available in
packages \pkg{methods} which is attached by default.  For objects
which have a formal class, its name is returned by \code{class}
as a character vector of length one.

The replacement version of the function sets the class to the value
provided.  For classes that have a formal definition, directly
replacing the class this way is strongly deprecated.  The expression
\code{\LinkA{as}{as}(object, value)} is the way to coerce an object to a
particular class.
\end{Section}
\begin{Note}\relax
Functions \code{oldClass} and \code{oldClass<-} behave in the same way
as functions of those names in S-PLUS 5/6, \emph{but} in \R{}
\code{\LinkA{UseMethod}{UseMethod}} dispatches on the class as returned by
\code{class} (with some interpolated classes: see the link) rather
than \code{oldClass}.  \emph{However}, \LinkA{group generic}{group generic}s dispatch
on the \code{oldClass} for efficiency.
\end{Note}
\begin{SeeAlso}\relax
\code{\LinkA{UseMethod}{UseMethod}}, \code{\LinkA{NextMethod}{NextMethod}},
\code{\LinkA{group generic}{group generic}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
x <- 10
inherits(x, "a") #FALSE
class(x) <- c("a", "b")
inherits(x,"a") #TRUE
inherits(x, "a", TRUE) # 1
inherits(x, c("a", "b", "c"), TRUE) # 1 2 0
\end{ExampleCode}
\end{Examples}

