R S3 Class

S3 class implements a style of OO (Object Oriented) programming called generic- function OO. This is totally different from most programming languages, like C++, Java, and C# which implement message passing OO. With message passing messages or methods are sent to objects and the object determines which function to call. Typically, this object has an appearance in the method call, usually appearing before the name of the method or message. Like, canvas.drawRect(“blue”).

S3 is different. While computations are still carried out via methods, a special type of function called a generic function decides which method to call, e.g., drawRect(canvas, “blue”).

Defining S3 Class and Create S3 Objects

S3 has no formal definition of classes.

Example:

The above example will create an S3 class with the given list.

Output:

Creating Objects using constructors

You can easily convert your R object (vector, matrix, array, factor, list, data frame, or anything else) in the object of certain class type by simply attaching the ‘class’ attribute.

It is a good practice to use a function with the same name as a class to create objects.

Example:

Let’s see an example, here we use the attr() function to set the class attribute of the object:

Let’s create an object using this constructor:

Output:

To know the class of object s:

If you enter the GPA which is not in between 0 and 10 then it will display the error:

To change the value of GPA:

Methods and Generic Functions

In the above example, when we simply write the name of the object, its contents get printed. Furthermore, we can use also use the print() function to print the contents of different type of objects like vectors, list, data frames, factors etc.

Now the question is how does print() know how to print these varieties of the dissimilar looking object? The answer is print() is a generic function. Actually, it has a number of methods. We can check all these methods with methods(print).

In the above list we can find the methods like print.data.frame and print.factor.

When we call print() on a data frame, it is go to print.data.frame(). And when we call the same with a factor, the call will go to print.factor(). Hence, we can see that the method names are in the form generic_name.class_name(). This is how R is able to find out which method to call depending on the class.

Printing our object of class “student” looks for method of the form print.student(), but there is no method of this form. So, for this print.default() method is used. print.default() is a fallback method which is called if no other match is found.

Generic functions have a default method. There are several generic functions like print(). You can list them all with methods(class = “default”).

Create our own Method

Let’s see an example to create a method print.student():

Now, this method will be called whenever we print an object of class “student”.

In S3 class system, method does not belong to object or class, they belong to generic functions. This will work as long as the class of object is set.

We can restore the class to their previous state by using unclass() function:

Creating our own Generic Function

In R, it is possible to create our own generic function like print().

Let’s see an example to create our own generic function called percent:

Here, useMethod() is used with the name of a generic function. This method is the dispatcher function which will handle all the background details.

Since a generic function is useless without any method. Let’s  implement the default method:

Now, let’s run this generic function:

Reference:
https://www.cyclismo.org/tutorial/R/s3Classes.html