[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  class: Object Classes

##  Description

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.

##  Usage

    
    
    class(x)
    class(x) <- value
    unclass(x)
    inherits(x, what, which = FALSE)
    
    oldClass(x)
    oldClass(x) <- value

##  Arguments

x

a  R  object

what, value

a character vector naming classes. ` value ` can also be ` NULL ` .

which

logical affecting return value: see ‘Details’.

##  Formal classes

An additional mechanism of _formal_ classes, nicknamed “S4”, is available in
package  methods  which is attached by default. For objects which have a
formal class, its name is returned by ` class ` as a character vector of
length one and method dispatch can happen on _several_ arguments, instead of
only the first. However, S3 method selection attempts to treat objects from an
S4 class as if they had the appropriate S3 class attribute, as does ` inherits
` . Therefore, S3 methods can be defined for S4 classes. See the ‘ [
Introduction ](/link/Introduction?package=base&version=3.6.2&to=methods) ’ and
‘ [ Methods_for_S3 ](/link/Methods_for_S3?package=base&version=3.6.2) ’ help
pages for basic information on S4 methods and for the relation between these
and S3 methods.

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 ` [ as
](/link/as?package=base&version=3.6.2) (object, value) ` is the way to coerce
an object to a particular class.

The analogue of ` inherits ` for formal classes is ` [ is
](/link/is?package=base&version=3.6.2) ` . The two functions behave
consistently with one exception: S4 classes can have conditional inheritance,
with an explicit test. In this case, ` is ` will test the condition, but `
inherits ` ignores all conditional superclasses.

##  Details

Here, we describe the so called “S3” classes (and methods). For “S4” classes
(and methods), see ‘Formal classes’ below.

Many  R  objects have a ` class ` attribute, a character vector giving the
names of the classes from which the object _inherits_ . (Functions ` oldClass
` and ` oldClass<- ` get and set the attribute, which can also be done
directly.)

If the object does not have a class attribute, it has an implicit class,
notably ` "matrix" ` , ` "array" ` , ` "function" ` or ` "numeric" ` or the
result of ` [ typeof ](/link/typeof?package=base&version=3.6.2) (x) ` (which
is similar to ` [ mode ](/link/mode?package=base&version=3.6.2) (x) ` ), but
for type ` "language" ` and ` [ mode ](/link/mode?package=base&version=3.6.2)
` ` "call" ` , where the following extra classes exist for the corresponding
function ` [ call ](/link/call?package=base&version=3.6.2) ` s: ` if ` , `
while ` , ` for ` , ` = ` , ` <- ` , ` ( ` , ` { ` , ` call ` .

Note that ` [ NULL ](/link/NULL?package=base&version=3.6.2) ` objects cannot
have attributes (hence not classes) and attempting to assign a class is an
error.

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

The function ` class ` prints the vector of names of classes an object
inherits from. Correspondingly, ` class<- ` sets the classes an object
inherits from. Assigni