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

Full Stack Developer Salary in India in 2024 [For Freshers & Experienced]
907173
Wondering what is the range of Full Stack Developer salary in India? Choosing a career in the tech sector can be tricky. You wouldn’t want to choose
Read More

by Rohan Vats

15 Jul 2024

SQL Developer Salary in India 2024 [For Freshers & Experienced]
902296
They use high-traffic websites for banks and IRCTC without realizing that thousands of queries raised simultaneously from different locations are hand
Read More

by Rohan Vats

15 Jul 2024

Library Management System Project in Java [Comprehensive Guide]
46958
Library Management Systems are a great way to monitor books, add them, update information in it, search for the suitable one, issue it, and return it
Read More

by Rohan Vats

15 Jul 2024

Bitwise Operators in C [With Coding Examples]
51783
Introduction Operators are essential components of every programming language. They are the symbols that are used to achieve certain logical, mathema
Read More

by Rohan Vats

15 Jul 2024

ReactJS Developer Salary in India in 2024 [For Freshers & Experienced]
902674
Hey! So you want to become a React.JS Developer?  The world has seen an enormous rise in the growth of frontend web technologies, which includes web a
Read More

by Rohan Vats

15 Jul 2024

Password Validation in JavaScript [Step by Step Setup Explained]
48976
Any website that supports authentication and authorization always asks for a username and password through login. If not so, the user needs to registe
Read More

by Rohan Vats

13 Jul 2024

Memory Allocation in Java: Everything You Need To Know in 2024-25
74112
Starting with Java development, it’s essential to understand memory allocation in Java for optimizing application performance and ensuring effic
Read More

by Rohan Vats

12 Jul 2024

Selenium Projects with Eclipse Samples in 2024
43493
Selenium is among the prominent technologies in the automation section of web testing. By using Selenium properly, you can make your testing process q
Read More

by Rohan Vats

10 Jul 2024

Top 10 Skills to Become a Full-Stack Developer in 2024
229997
In the modern world, if we talk about professional versatility, there’s no one better than a Full Stack Developer to represent the term “versatile.” W
Read More

by Rohan Vats

10 Jul 2024

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