Encapsulation in R Programming

In R programming, OOPs provides classes and objects as its key tools to reduce and manage the complexity of the program. R is a functional language that uses concepts of OOPs. We can think of class like a sketch of a car. It contains all the details about the model_name, model_no, engine, etc. Based on these descriptions we select a car. The car is the object. Each car object has its own characteristics and features. An object is also called an instance of a class and the process of creating this object is called instantiation. OOPs has the following features:

  • Class
  • Object
  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance

oops in R

So in this article let’s discuss Encapsulation features in R programming.

Encapsulation in R

While working with classes and dealing with sensitive data, implementing global access to all the variables used within the program code is not a good choice because then the chances of getting the data tempered will increase. For this purpose, Object-Oriented Programming languages have the concept of Encapsulation. Encapsulation ensures that the outside view of the object is clearly separated from the inside view of the object by hiding the implementation of operation and state from the interface which is available to all other objects. 

Note: An object is a collection of operations that shares a state. The collection of operations define the behavior of an object. 



Encapsulation

Encapsulation is: 

  • Binding the data with the code that manipulates it. 
  • It keeps the data and the code safe from external interference 

A common way to think about encapsulation is like when you go to a restaurant the waiter comes and asks you about what you want to eat and then you order the waiter with what you want to eat, and the waiter then delegates the cooking of what you requested to the restaurant’s chef and brings you the ordered dish. You cannot directly go to the chef and tell what you want to eat. This will not be appropriate for the restaurant and for you. The same goes for the class, when you want two classes to communicate with one another it is actually the objects of those classes communicating with each other and doing the work. No two classes can directly communicate with each other because then data becomes vulnerable and easy to temper. Just like this Updating, modifying, or deleting data from variables can be done through the use of methods that are defined specifically for the purpose. The benefit of using this approach to programming is improved control over the input data and better security. Encapsulation helps to create code that is loosely coupled. Because the details are hidden, it reduces the ability of other objects to directly modify an object’s state and behavior. 

Example 1: 

R

filter_none

edit
close

play_arrow

link
brightness_4
code

l <- list(name = "Geeksforgeeks"
          state = "UP", sector= 136)
  
# Class name declared as 'info'
class(l) <- "info" 
  
# Object creation of class 'info'
l

chevron_right


Output: 

$name 
[1] "Geeksforgeeks" 

$state 
[1] "UP" 

$sector 
[1] 136 

attr(, "class") 
[1] "info" 

Example 2: 

R

filter_none

edit
close

play_arrow

link
brightness_4
code

s <- list(country = "India",  
          state = "Delhi", street_no.= 110) 
    
# Class name declared as 'address'   
class(s) <- "address"
    
# Object creation of class 'address'. 
s

chevron_right


Output: 

$country
[1] "India"

$state
[1] "Delhi"

$street_no.
[1] 110

attr(, "class")
[1] "address"

Analogy such as this would help to understand better – When a user orders something from a website, he just wants to pay and receive his product. It won’t be a good thing to let the user know the internal working of the system that how his order is placed and where it goes. While using any encapsulated code like above you should know the following things: 

  1. Everyone knows how to access it. 
  2. Can be easily used regardless of implementation details. 
  3. There shouldn’t any side effects of the code, to the rest of the application. 

Encapsulation can help to build more maintainable code by helping to block the ripple effect of code changes. It also helps with creating loosely coupled code by diminishing direct access to an object’s state and behavior.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.