Object-oriented programming

Introduction to S3, S4, or reference class with generics and methods
Author
Affiliation

Department of Biostatistics, Johns Hopkins

Published

November 21, 2024

Pre-lecture activities

Important

In advance of class, please read through:

In addition, please have a brief look through these four sections of the book:

Lecture

Acknowledgements

Material for this lecture was borrowed and adopted from

Learning objectives

Learning objectives

At the end of this lesson you will:

  • Recognize the primary object-oriented systems in R: S3, S4, R6, and Reference Classes (RC).
  • Understand the terminology of a class, object, method, constructor and generic.
  • Be able to create a new S3 or S4 with generics and methods

Slides

Class activity

For the rest of the time in class, you and your team will work on the final project. Stephanie will walk around to answer questions and to help answer questions and support team projects.

Post-lecture

Summary

  • R has three object oriented systems: S3, S4, and Reference Classes.
  • Reference Classes are the most similar to classes and objects in other programming languages.
  • Classes are blueprints for an object.
  • Objects are individual instances of a class.
  • Methods are functions that are associated with a particular class.
  • Constructors are methods that create objects.
  • Everything in R is an object.
  • S3 is a liberal object oriented system that allows you to assign a class to any object.
  • S4 is a more strict object oriented system that build upon ideas in S3.

Additional practice

Here are some additional practice questions to help you think about the material discussed.