Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconWhat is Data Hiding In C++? Abstraction and Encapsulation Explained

What is Data Hiding In C++? Abstraction and Encapsulation Explained

Last updated:
24th May, 2021
Views
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
What is Data Hiding In C++? Abstraction and Encapsulation Explained

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.  

Check out our free courses to get an edge over the competition.

Ads of upGrad blog

Explore Our Software Development Free Courses

Data Hiding, Data Abstraction and Data Encapsulation

Data hiding in C++ is closely correlated to two other OOP properties- abstraction and encapsulation. 

Data Abstraction

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. 

Check out upGrad’s Java Bootcamp

Explore our Popular Software Engineering Courses

Data Encapsulation 

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. 

Check out upGrad’s Advanced Certification in Blockchain

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.

  1. 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. 
  2. 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.
  3. 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. 
  4. In data hiding, the data has to be defined as private only. In data encapsulation, the data can be public or private. 
  5. Data hiding is both a process and technique in itself, whereas data encapsulation is a sub-process in data hiding. 

In-Demand Software Development Skills

Access Specifiers

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. 

  1. 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. 
  2. 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:

  1. It is used to reduce data unpredictability and data complexity.
  2. It improves the reusability of the program. 
  3. By limiting interdependencies between software components, also reduces system complexity for increased robustness. 
  4. Hides the physical stockpiling design for the data. This helps in defining the interface clearly, improves readability and comprehensibility.
  5. 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. 
  6. The encapsulated classes are straightforward, easy to manage and facilitates future development of the application. 

Read our Popular Articles related to Software Development

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

 

The Disadvantage Of Data Hiding 

Ads of upGrad blog

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. 

Learn Software Engineering Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Conclusion

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.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What is encapsulation in object-oriented programming?

Encapsulation is a way of binding together the data and the code that operates on the data. It is a mechanism used to hide the data implementation details while exposing the data abstraction to the outside world. The process of hiding information behind a well-defined interface is known as encapsulation. Encapsulation is a powerful concept of object-oriented programming. It is the process of hiding internal data and operations of an object from the outside world by creating accessor functions.

2What is abstraction in object-oriented programming?

Abstraction is a technique used in Object-Oriented Programming to separate the interfaces of classes from the implementation details of each individual object. This means that the code can be changed without altering the code which uses the class. In object-oriented programming (OOP), an abstraction represents an object that can be manipulated, but not explicitly defined. Abstraction is an important part of OOP; it is the process of separating the interface of a class from the implementation of its methods.

3How does classes and objects interact in object-oriented programming?

Classes and objects are the foundation of object-oriented (OO) programming. Object-oriented programming is a programming paradigm, which uses objects and classes to model the real-world. A class is a blueprint or a template. It's a description or a specification of what an object should be. It defines the characteristics, behaviors and data of a particular type of object. Objects are instances or examples of a class. Each and every object in a program can be classified into one or more classes. A class can inherit the properties, methods and other characteristics of another class. This is called as inheritance.

Explore Free Courses

Suggested Blogs

Top 7 Node js Project Ideas & Topics
31572
Node.JS is a part of the famous MEAN stack used for web development purposes. An open-sourced server environment, Node is written on JavaScript and he
Read More

by Rohan Vats

05 Mar 2024

How to Rename Column Name in SQL
46926
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers & Experienced]
901322
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners & Experienced]
52082
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers & Experienced]
909178
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34744
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers & Experienced]
902385
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
26215
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions & Answers [2024]
4382
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon