chevron_left
399 points
8 4 3

Difference between encapsulation and data hiding

Key Difference – Data Hiding vs Encapsulation
 

Object oriented Programming is a major paradigm in programming. It helps to develop the program or the software using objects. Objects are created using a blueprint. It is called a class. A class consists of attributes and behaviors that should include in the object. A class contains data members and methods. The data members describe the attributes of the object while methods describe the behavior of the object. Data Hiding and Encapsulation are two concepts of OOP. Data hiding is the process of protecting the members of the class from unauthorized access while Encapsulation is the process of wrapping the data members and methods into a single unit. This is the key difference between data hiding and encapsulation. Data hiding focus on securing the data while hiding the complexity of the system. Encapsulation mainly focuses on hiding the complexity of the system. Encapsulation is a way of achieving data hiding.

CONTENTS

What is Data Hiding?

The class contains data members and methods. Data hiding is the process of protecting the members of the class. Therefore, it is the mechanism to improve security. In programming languages such as Java, use access modifiers. They are public, private and protected. The public data members and methods are accessible by objects of other classes. The protected members are accessible by the objects of the same class and its subclass. The private members are accessible by the objects within the class.

The programmer can use these access modifiers according to the application. If it is not necessary to restrict accessing the members, he can use a public modifier. Inheritance is a concept of OOP. Rather than, writing the program from the beginning, the programmer can use already existing classes. The existing class is the superclass while the new class is called the subclass. The programmer can make the members of the class only accessible to that class and related subclasses using ‘protected’. If it is required to restrict accessing the data from outside the class, the modifier ‘private’ can be used.

Data hiding is to prevent other objects from accessing the members of a specific class. Therefore, the programmer should use the private access modifier. Then, the data members are only accessible through methods. If there is a class called Account and if it contains a data member as balance, that data member should not be accessible only to that class. Therefore, it can make the balance, which is a private member. Now it is only accessible within the class. This improves data security.

 

What is Encapsulation?

In OOP, the program or the software can be modeled using objects. Each object has attributes and behaviors. The attributes are the data members or properties and the behaviors are methods. Each object is created using a class. It provides the blueprint or a description to build objects. Encapsulation is one major pillar belongs to OOP. It is the process of combining the data members and methods into one single unit.

This grouping of data members and methods can make the program manageable and also reduces the complexity. A Rectangle class can have data members such as width, length. It can have methods such as getDetails, getArea, and display. All the data members and methods are combined into a single class called Rectangle. In Encapsulation private, protected,  public modifiers can be used. The access modifiers help to protect data. Encapsulation can be defined as a way of achieving data hiding.

Difference Between Data Hiding and Encapsulation

What is the Similarity Between Data Hiding and Encapsulation?

  • Both Data Hiding and Encapsulation are concepts related to Object Oriented Programming (OOP).

What is the Difference Between Data Hiding and Encapsulation?

Data Hiding vs Encapsulation

Data hiding is the process that ensures exclusive data access to class members and projects object integrity by preventing unintended or intended changes.Encapsulation is an OOP methodology, that bundles the data with the methods operating on that data.
 Main Focus
Data hiding focuses on securing the data while hiding the complexity.Encapsulation focus on hiding the complexity of the system.
Methodology
Data hiding is data protecting process.Encapsulation is a method of achieving data hiding.
 Access Modifiers
Data hiding uses private access modifier.Encapsulation uses private, protected, public access modifiers.

Summary – Data Hiding vs Encapsulation

Data Hiding and Encapsulation are two concepts of OOP. Data hiding is the process of protecting the members of the class from unauthorized access. Encapsulation is the process of wrapping the data members and methods into a single unit. That is the difference between data hiding and encapsulation. Encapsulation is a way of achieving data hiding.

1 Comment

I've been looking for something like this for a long time. Thank you for making this available.

More Posts

Difference between Abstraction and Encapsulation in C# with example mhfaisal - May 7
Difference between abstraction and encapsulation in c# with a real-time example. Farkhanda Athar - Sep 1, 2020
What is the difference between pre and post Sanjana Sagar - May 20
What is the difference between oltp and olap Sanjana Sagar - May 20
What's the difference between alpha and beta Sanjana Sagar - May 20
What is the difference between div and span amna - Jun 26
Difference between virtual function and pure virtual function in C++ Sanjana Sagar - Jun 22
Difference between a library and a framework Sanjana Sagar - May 29
Difference between for and while loop Sanjana Sagar - May 29
What is the difference between padding and margin Sanjana Sagar - May 28