Loading, please wait...

A to Z Full Forms and Acronyms

What is the R Object-Oriented Programming?

Dec 02, 2021 #RLanguage #Programming, 2858 Views
In this article, we will learn about:What Is Object-Oriented Programming? What are Object and Class?What are different concepts in OOPs?What is the S3 class?How to define the S3 class?What is an attribute?

What is R Object-Oriented Programming?

In this article, you will learn about:

  • What Is Object-Oriented Programming? 
  • What are Object and Class?
  • What are different concepts in OOPs?
  • What is the S3 class?
  • How to define the S3 class?
  • What is an attribute?

What Is Object-Oriented Programming?

Object-Oriented Programming is a part of the programming paradigm. It contains data in the form of fields and procedures. OOPs helps to reduce and manage the complexity of the program upto some extent. R language has S3 and S4 classes. 

Let's talk about the different modules in object-oriented programming:

Object

It is the smallest and the basic unit of object-oriented programming. The Object is a collection of data and functions that club into units.

Class

It is the blueprint of the object. Class defines the properties of the object. For example, a car is an object. The properties of the car are the model_name, model_number, engine, company_name, and more. 

We have S3 and S4 classes in the R language. 

Object-oriented programming has a few basic concepts. They are:

  • Abstraction: Abstraction is a process of presenting only required information to the outside world and hiding all the background details. For example, the compiler processes the data internally and gives us the output only. 
  • Encapsulation: It is a process of binding the data and functions into one unit. 
  • Inheritance: The capacity of creating a new class with the help of an existing class is known as Inheritance. 
  • Polymorphism: In general meaning, the poly means multiple. So, polymorphism means we can create multiple functions with the same name but different return types, different numbers of arguments, and different data types of arguments.

What is the S3 class?

S3 is the simplest but popular OOP class. It doesn't have any proper definition and format.

Creating the S3 class

To create the object of the S3 class, create a list that will have all the class members. After this pass, the list in the class() method as an argument. 

Syntax of creating the S3 class:

v_name <- list(a1, a2, a3….aN)

Here, v_name is the variable name, and a1, a2, a3 are the attributes. 

Example:

In the below code, the employee is the class. The attribute of employee class is name and empid.

# List creation with its attributes name and empid.

e < - list(name="Joe", empid=E10)

# Defining a class "Student"

class(e) < - "Employee" 

# Creation of object

e

When we run the following code, the expected output is:

$name

[1] "Joe"

$Roll_No

[1] E10

attr(, "class")

[1] "Employee"

Attribute

The attribute is only the extra information handled by objects. It does not affect the value of an object. We can use the attributes() method to check the attribute of an object.

Syntax:

attribute(object)

Example:

Let's display the attributes of the Employee class.

attributes(a)

The output of the following statement is:

$names

'name''empid'

$class

'Employee'
A to Z Full Forms and Acronyms