Data Abstraction in Ruby
The idea of representing significant details and hiding details of functionality is called data abstraction. The interface and the implementation are isolated by this programming technique. Data abstraction is one of the object oriented programming features as well. Abstraction is trying to minimize information so that the developer can concentrate on a few ideas at a time. Abstraction is the foundation for software development.
Consider a real-life example of making a phone call. The only thing the person knows is that typing the numbers and hitting the dial button will make a phone call, they don’t know about the inner system of the phone or the dial button on the phone. That’s what we call abstraction.
Another real-life example of abstraction is as users of television sets, we can switch it on or off, change the channel and set the volume without knowing the details about how its functionality has been implemented.
Data Abstraction in modules: In Ruby, Modules are defined as a set of methods, classes, and constants together.For example, consider the sqrt() method present in Math module. Whenever we need to calculate the square root of a non negative number, We simply call the sqrt() method present in the Math module and send the number as a parameter without understanding the actual algorithm that actually calculates the square root of the numbers.
Data Abstraction in Classes: we can use classes to perform data abstraction in ruby. The class allows us to group information and methods using access specifiers (private, protected, public). The Class will determine which information should be visible and which is not.
Data Abstraction using Access Control: There are three levels of access control in Ruby (private, protected, public). These are the most important implementation of data abstraction in ruby. For Example-
- Members who have been declared public in a class can be accessed from anywhere in the program.
- Members declared to be private in a class can only be accessed from within the class. They are
not allowed to access any part of the code outside the class.
In Public! In Private!
In the above program, we are not allowed to access the privateMethod() of Geeks class directly, however, we can call the publicMethod() in the class in order to access the privateMethod().
Advantages of Data Abstraction:
- Helps increase the security of a system because only crucial details are made available to the user.
- It increases re-usability and prevents redundancy of code.
- Could alter the internal class implementation independently without affecting the user.
Please Login to comment...