Introduction To Data Hiding
Data is the most sensitive and volatile component of a program, which, if manipulated, can result in an incorrect output and harm the integrity of data. This is where data hiding proves essential. In technical jargon, data hiding also referred to as information hiding, is an inherent object-oriented programming mechanism (OOP) to hide internal object details from the end-user.
Data hiding’s underlying objective is to conceal data within a class from unauthorized access and avoid unnecessary penetration from outside the class. Data hiding guarantees constrained data access to ensure object integrity and prevent unintended or intended changes to the program in question.
Simply put, data hiding takes certain parts of a program code and screens it from object members. If an object member is trying to access hidden data, the program will return an error. This is a cautionary mechanism introduced to ensure that the programmer abstains from connecting to erroneous data that has been hidden. It is often the internal components with no foreseeable need on the user’s end that are hidden.
Data Hiding, Data Abstraction and Data Encapsulation
Data hiding in C++ is closely correlated to two other OOP properties- abstraction and encapsulation.
Data abstraction is a mechanism to expose only the relevant program interface to the end-user and hiding the more intricate and complex implementation details. Let’s consider a real-life example for better understanding. Consider your television. You can turn the television on and off, change the channel, adjust its volume and add external components such as VCRs, DVD players and speakers. But you do not know the internal mechanisms of the television.
You are oblivious to how it receives signals, translates them, and finally displays the output. Thus, television is an adept example of the separation between internal implementation and external influence. With data abstraction, class implementation is protected from inadvertent errors and evolves in response to changing requirements or bug reports without user-level interference.
Data encapsulation refers to the bundling of data and their related functions into a single unit called class. Put, if you have an invisible attribute from the outside of an object and bundle it with methods that provide read or write access to it, you can hide delicate information and regulate access to the internal state of the thing.
Therefore, depending on the methods you implement, you are at an onus to decide if an attribute can be read and changed or if it’s read-only or not visible at all.
Difference between data hiding and data encapsulation
In discussing OOP, data hiding and data encapsulation are often used interchangeably because both these notions cooperate to accomplish a common objective-auxiliary gatekeeper of sensitive information. The idea of data encapsulation is undoubtedly functionally comparative to data hiding, but since they work at different levels, they are structurally different.
Although intrinsically related to each other, there exists a crucial difference between data hiding and data encapsulation.
- The main difference between data hiding and encapsulation is that the former focuses on enhancing data security in the program, while the latter deals with hiding the program’s complexity.
- Data hiding concentrates on the accessibility of an object member within a class, while data encapsulation focuses on how the data is accessed and how different objects behave. Encapsulation is mainly accomplished through information hiding and not just information concealing. This means that the programmer hides the object member’s structures and hides the implementation of all its methods.
- While data hiding focuses on restricting data use in a program to assure data security, data encapsulation focuses on wrapping (or encapsulating) the complex data to present a simpler view to the user.
- In data hiding, the data has to be defined as private only. In data encapsulation, the data can be public or private.
- Data hiding is both a process and technique in itself, whereas data encapsulation is a sub-process in data hiding.
C++ supports data hiding, and by extension, data abstraction and data encapsulation through the creation of user-defined types, known as classes. The scope of members of this created class is defined by keywords known as access specifiers. Typically, there are three types of protection or access specifiers available within a category- private, protected and public, used to build the class’s encapsulation capabilities.
Access specifiers help define how variables and functions of a type can be accessed from outside the class. Usually, the data within a class is private to eliminate accidental manipulation instances, and its operations are public. However, accessibility within a rank is not bound by any restriction.
Private variables/functions: Can only be accessed by members defined as part of the class.
Public variables/functions: Can be accessed from anywhere in the program.
Protected variables/functions Are private within a class and are only available for remote access in the derived class.
Application Of Data Hiding
To better understand data hiding, let us consider the following example. Considering you are the programmer, let us assume that you declare a class by the name ‘CheckAccount,’ in which you have defined a data member by the term ‘Balance,’ which refers to a user’s bank account balance.
In this particular example, the data member ‘Balance’ is sensitive information. Although you might offer outside application access to view this sensitive information, in all likelihood, you will not let this exterior application alter the attributes of the data stored in the member’ Balance.’ This outcome can be achieved using data hiding, and more specifically, is controlled by the use of the private access specifier.
- Data hiding is commonly executed upon data that is unpredictable and delicate. This kind of data is fundamental in running a program effectively and quickly. Due to unduly access, resulting data changes data are permanent and mandate complete rework on the programmer’s part before further use.
- Data hiding helps protect class members against unintentional overlooks. A class usually comprises various related fields that must be in a reliable state. Suppose a programmer is allowed to control any of these fields straightforwardly. In that case, there is a possibility that you might transform one area without changing critical related fields, leaving your class in a conflicted state.
Benefits Of Data Hiding
The benefits of data hiding are multifold:
- It is used to reduce data unpredictability and data complexity.
- It improves the reusability of the program.
- By limiting interdependencies between software components, also reduces system complexity for increased robustness.
- Hides the physical stockpiling design for the data. This helps in defining the interface clearly, improves readability and comprehensibility.
- It provides data assurance from corruption and unwarranted access. In other words, it helps to conceal important data, thereby ensuring heightened security protocols against hackers. If all internal data were to be made public, hackers could easily penetrate the internal data and make maliciously intended changes to alter the program’s functioning. Hiding the data makes it much harder to crack the code because the hidden data will appear invisible to outside members of the class and, therefore, the hacker.
- The encapsulated classes are straightforward, easy to manage and facilitates future development of the application.
The Disadvantage Of Data Hiding
The only disadvantage of data hiding is extra coding. Data hiding necessitates programmers to write lengthier codes to create the desired effect in hidden data.
Data hiding is akin to a game of Russian Roulette. In Russian Roulette, only one out of six holes have a bullet. Let us imagine the shot is a perceivable data threat. Would you be willing to take a chance? There might exist five scenarios that work in your favour, but can you specifically identify the threat? The answer is no. All the scenarios bear an equal probabilistic burden of suspicion and fear. Instead of giving in to this uncertainty, take a stand against data threats. Go undercover with data hiding.
If you’re interested to learn more about Java, OOPs & full-stack software development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.