Chapter 10. Object-Oriented Programming

In the previous chapter, you learned how functional programming and metaprogramming make it possible to customize the behavior of functions. We can create a function within a certain context, which is called a closure. We can also use higher order functions by passing functions around just like other objects.

In this chapter, you will learn how to customize the behavior of objects by walking into the world of object-oriented programming. R provides several different object-oriented systems to work with. At first glance, they look quite different from the object-oriented systems in other programming languages. However, the idea is mostly the same. I will briefly explain the concept of class and method of objects, and show you how they can be useful in unifying the way we work with data and models.

We will cover the following topics at the beginner level in the subsequent sections:

  • The idea of object-oriented programming
  • S3 system
  • S4 system
  • Reference class
  • R6 package

Finally, we will compare these systems in several aspects.

Introducing object-oriented programming

If you are a developer from programming languages such as Java, Python, C++, C#, you should feel familiar with the object-oriented style of coding. However, if you are not familiar with any other object-oriented programming languages, you will probably be puzzled by this term, as it sounds a bit abstract. However, don't worry; this is much easier to understand than it looks if we think about the core of programming.

When we talk about programming, we are actually talking about using programming tools to solve problems. Before solving the problem, we need to model the problem first. Traditionally, we usually figured out an algorithm that takes several steps to solve a numeric computing problem. Then, we wrote some procedural code to implement the algorithm. For example, most statistical algorithms are implemented in a procedural style, that is, by transforming the input into the output according to the theory, step by step.

However, many problems are so closely bounded to the real world that it can be very intuitive to model the problem by defining some classes of objects as well as the interaction between them. In other words, by programming in an object-oriented style, we simply try to mimic the important features of the objects in concern at an appropriate level of abstraction.

There are many concepts involved in object-oriented programming. Here, we will only focus on the most important ones.

Understanding classes and methods

The most important concepts in this chapter are classes and methods. A class describes what the object is, and a method defines what it can do. There are countless real-world examples for these concepts. For example, animal can be a class. In this class, we can define methods such as make sound and move. The vehicle can be a class, too. In this class, we can define methods such as start, move, and stop. The person can be a class that has methods such as wake up, talk to another person, and go somewhere.

For a particular problem, we can define classes according to our need to model the objects we are dealing with and define methods for them to model the interaction between the objects. The objects need not be physical or tangible. One practical example is a bank account. It only exists in the data storage of banks, but it can be useful to model bank accounts with some data fields such as balance and owner, and some methods such as deposit, withdraw, and transfer between two accounts.

Understanding inheritance

Another important concept of object-oriented programming is inheritance, that is, we can define a class that inherits the behavior of a base (or super) class and has some new behavior. Usually, the base class is more abstract and general in concept, and the inheriting class is more concrete and specific. This is simply true for the concepts in our everyday life.

For example, dog and cat are two classes that inherit from the animal class. The animal class defines methods such as make sound and move. The dog and cat classes inherit these methods but implement them in different ways so that they make different sounds and move in different manners.

Also, carbus, and airplane are classes that inherit from the vehicle class. The vehicle class defines methods such as startmove, and stop. The carbus, and airplane classes inherit these functionalities but work in different ways. The car and bus can move in two dimensions on the surface, while airplane can move in three dimensions in the air.

There are some other concepts in the system of object-oriented programming, but we are not going to focus on them in this chapter. Let's keep in mind the concepts we mentioned and see how these concepts work in R programming.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset