Key Difference – Data Hiding vs Encapsulation
Object-Oriented Programming (OOP) 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.
1. Overview and Key Difference
2. What is Data Hiding
3. What is Encapsulation
4. Similarities Between Data Hiding and Encapsulation
5. Side by Side Comparison – Data Hiding vs Encapsulation in Tabular Form
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.
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.|
|Data hiding focuses on securing the data while hiding the complexity.||Encapsulation focus on hiding the complexity of the system.|
|Data hiding is data protecting process.||Encapsulation is a method of achieving data hiding.|
|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.“Encapsulation (Computer programming).” Wikipedia, Wikimedia Foundation, 28 Feb. 2018. Available here
2.“What is Data Hiding? – Definition from.” Techopedia.com. Available here
3.Point, Tutorials. “C# Encapsulation.” , Tutorials Point, 6 Dec. 2017. Available here