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:
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.
- 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.
$name  "Geeksforgeeks" $state  "UP" $sector  136 attr(, "class")  "info"
$country  "India" $state  "Delhi" $street_no.  110 attr(, "class")  "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:
- Everyone knows how to access it.
- Can be easily used regardless of implementation details.
- 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.