Difference Between Data Hiding and Encapsulation



Data Hiding and Encapsulation are important concepts in object-oriented programming (OOP). Data hiding helps prevent the illegal or unauthorized access of members of a class, while encapsulation helps in the wrapping up of data members and methods inside a class. The most basic difference between data hiding and encapsulation is that data hiding focuses on data security, while encapsulation focuses on hiding the complexity of the system.

Read this article to find out more about Data Hiding and Encapsulation and how they are different from each other. Let's start with a basic overview of these two important concepts, Data Hiding and Encapsulation.

What is Data Hiding?

In object-oriented programming (OOP), data hiding is one of the important concepts that ensures the security of members of a class from illegal access. Therefore, data hiding protects the data members from being changed or hacked. As we know, data is the most sensitive and volatile content of a program, and the manipulation of this data can produce an incorrect result. Also, it may affects the integrity of the data.

Now, let us consider an example to understand the concept of data hiding. Let we declared a BankAccount class and we have a data member AcBalance inside this class. Here, the account balance is a sensitive information. In this example, we may allow an external application to check the account balance, but we would not allow the application to change the account balance. Hence, in this case, we restrict the alternation in account balance by declaring the account balance attribute private. The major advantage of data hiding is that it reduces the complexity of the system.

What is Encapsulation?

In object-oriented programming, Encapsulation is a method that helps wrapping up of data into a single module. Encapsulation brings together the data and the methods that manipulate the data into a single component and protects them from outside interference. Encapsulation mainly involves bundling the data as well as the functions that use the data.

Data encapsulation leads to the very important concept of data hiding. In short, encapsulation is a method of binding the data and functions together in a capsule to hide the complexity of a class. In encapsulation, the members inside a class can be public, protected, or private. The encapsulation is primarily used to make the system more user-friendly.

Difference between Data Hiding and Encapsulation

The following table highlights all the important differences between data hiding and encapsulation −

Data Hiding Encapsulation
It is associated with data security. It can be defined as the wrapping up of data into a single module.
It also helps conceal the complexities of the application. This will hide the complicated and confidential information about the application.
It focuses on hiding/restricting the data usage. It focuses on hiding the complexity of the system.
It is considered as a process and a technique. It is considered as a sub−process in the bigger process of data hiding.
The data is always private and inaccessible. The encapsulated data can be private or public, depending on the requirement.

Conclusion

The most significant difference between these two concepts is that data hiding provides security unauthorized access of data, while encapsulation reduces the complexity of the system to make the application more user−friendly and fast.


Advertisements