9
OBJECT-ORIENTED PROGRAMMING
Many programmers believe that object-
oriented programming (OOP) makes for
clearer, more reusable code. Though very
different from the familiar OOP languages like
C++, Java, and Python, R is very much OOP in outlook.
The following themes are key to R:
- Everything you touch in R—ranging from numbers to character strings
to matrices—is an object. - R promotesencapsulation, which is packaging separate but related data
items into one class instance. Encapsulation helps you keep track of
related variables, enhancing clarity. - R classes arepolymorphic, which means that the same function call leads
to different operations for objects of different classes. For instance, a
call toprint()on an object of a certain class triggers a call to a print
function tailored to that class. Polymorphism promotes reusability. - R allowsinheritance, which allows extending a given class to a more spe-
cialized class.