Difference Between Data Hiding and Encapsulation

, , No Comments

 


Data hiding and encapsulation both are the important concept of object oriented programming. Encapsulation means wrapping the implementation of data member and methods inside a class. When implementation of all the data member and methods inside a class are encapsulated, the method name can only describe what action it can perform on an object of that class. Data Hiding means protecting the members of a class from an illegal or unauthorized

The main difference between data hiding and encapsulation is that data hiding focus more on data security and encapsulation focuses more on hiding the complexity of the system. There some other differences between data hiding and encapsulation they are described in the comparison chart shown below.

Content: Data Hiding Vs Encapsulation

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

BASIS FOR COMPARISONDATA HIDINGENCAPSULATION
BasicData hiding concern about data security along with hiding complexity.Encapsulation concerns about wrapping data to hide the complexity of a system.
FocusData Hiding focuses on restricting or permitting the use of data inside the capsule.Encapsulation focuses on enveloping or wrapping the complex data.
Access SpecifierThe data under data hiding is always private and inaccessible.The data under encapsulation may be private or public.
ProcessData hiding is a process as well as technique.Encapsulation is a sub-process in data hiding.

Definition of Data Hiding

Data hiding is a concept in object-oriented programming which confirms the security of members of a class from unauthorized access. Data hiding is a technique of protecting the data members from being manipulated or hacked from any other source. Data is the most sensitive and volatile content of a program which if manipulated can result in an incorrect output and it also harms the integrity of the data.

Data hiding is controlled in Java with the help of access modifiers (private, public and protected). The data which is public is accessible from outside the class hence if you want to hide your data or restrict it from being accessed from outside, declare your data private. Private data is only accessible to the objects of that class only.

Let us understand data hiding with the help of an example. Suppose you declared a CheckAccount class and you have a data member balance inside that class. Here, the balance of an account is sensitive information. You may allow an outside application to check balance inside of an account but,  you won’t allow an outside application to alter the balance attribute. Thus declaring the balance attribute private you would restrict the access to balance from an outside application.

Data hiding also reduces some complexity of the system. Data hiding can be achieved through the encapsulation, as encapsulation is a subprocess of data hiding.

Definition of Encapsulation

Encapsulation is binding the code and data together in a capsule to hide the complexity of a class. Encapsulation has less to do with access specifiers (private, public and protected).  In encapsulation members inside a class can be private, public or protected.

The private members of a class are only accessible to the objects of that class only, and the public members are accessible to the objects of the class as well as they are accessible from outside the class. Encapsulation helps the end user of a system to learn what to do with the system instead of how it must do.

Let us understand encapsulation with the help of an example of a car. If a driver of a car wants to change the gear of car, what he needs is to just change the position of the liver operating gears of the car and it thus changes the gear of a car. A driver does not need to understand the complexity of, what is the mechanism behind changing the gear. This is how encapsulation reduces the complexity of a system. Encapsulation makes the system easier to operate by the end user.


0 टिप्पणियाँ:

Post a Comment