(R-lang.info)Object-oriented programming


Next: Computing on the language Prev: Functions Up: Top
Enter node , (file) or (file)node

5 Object-oriented programming
*****************************

Object-oriented programming is a style of programming that has become
popular in recent years.  Much of the popularity comes from the fact
that it makes it easier to write and maintain complicated systems.  It
does this through several different mechanisms.

   Central to any object-oriented language are the concepts of class and
of methods.  A _class_ is a definition of an object.  Typically a class
contains several _slots_ that are used to hold class-specific
information.  An object in the language must be an instance of some
class.  Programming is based on objects or instances of classes.

   Computations are carried out via _methods_.  Methods are basically
functions that are specialized to carry out specific calculations on
objects, usually of a specific class.  This is what makes the language
object oriented.  In R, _generic functions_ are used to determine the
appropriate method.  The generic function is responsible for determining
the class of its argument(s) and uses that information to select the
appropriate method.

   Another feature of most object-oriented languages is the concept of
inheritance.  In most programming problems there are usually many
objects that are related to one another.  The programming is
considerably simplified if some components can be reused.

   If a class inherits from another class then generally it gets all the
slots in the parent class and can extend it by adding new slots.  On
method dispatching (via the generic functions) if a method for the class
does not exist then a method for the parent is sought.

   In this chapter we discuss how this general strategy has been
implemented in R and discuss some of the limitations within the current
design.  One of the advantages that most object systems impart is
greater consistency.  This is achieved via the rules that are checked by
the compiler or interpreter.  Unfortunately because of the way that the
object system is incorporated into R this advantage does not obtain.
Users are cautioned to use the object system in a straightforward
manner.  While it is possible to perform some rather interesting feats
these tend to lead to obfuscated code and may depend on implementation
details that will not be carried forward.

   The greatest use of object oriented programming in R is through
'print' methods, 'summary' methods and 'plot' methods.  These methods
allow us to have one generic function call, 'plot' say, that dispatches
on the type of its argument and calls a plotting function that is
specific to the data supplied.

   In order to make the concepts clear we will consider the
implementation of a small system designed to teach students about
probability.  In this system the objects are probability functions and
the methods we will consider are methods for finding moments and for
plotting.  Probabilities can always be represented in terms of the
cumulative distribution function but can often be represented in other
ways.  For example as a density, when it exists or as a moment
generating function when it exists.

Definition
Inheritance
Method dispatching
UseMethod
NextMethod
Group methods
Writing methods

automatically generated by info2www version 1.2.2.9