Introduction to Data Encapsulation

Data encapsulation, also known as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a restricted set of operations on the hidden members of the class by executing special functions commonly called methods.

The advantage of using data encapsulation comes when the implementation of the class changes but the interface remains the same. For example, to create a stack class which can contain integers, the designer may choose to implement it with an array, which is hidden from the user of the class. The designer then writes the push() and pop() methods which puts integers into the array and removes them from the array respectively. These methods are made accessible to the user. Should an attempt be made by the user to access the array directly, a compile time error will result. Now, should the designer decide to change the stack’s implementation to a linked list, the array can simply be replaced with a linked list and the push() and pop() methods rewritten so that they manipulate the linked list instead of the array. The code which the user has written to manipulate the stack is still valid because it was not given direct access to the array to begin with.

The concept of data encapsulation is supported in C++ through the use of the public, protected and private keywords which are placed in the declaration of the class. Anything in the class placed after the public keyword is accessible to all the users of the class; elements placed after the protected keyword are accessible only to the methods of the class or classes derived from that class; elements placed after the private keyword are accessible only to the methods of the class.

As a convention, calling a method of an object instantiated from a class is commonly referred to as sending a message to that object.

 

 

 

Encapsulation has several advantages, they are:

  • Code is more flexible and easy to change with new requirements.
  • In Java, Encapsulation makes unit testing easy.
  • In Java, Encapsulation allows you to control who can access what.
  • It also helps to write unchangeable classes or objects.
  • It reduces coupling of modules and increases cohesion inside a module because all piece of one thing are encapsulated in one place.
  • It allows you to change one part of code without affecting other part of code.

Encapsulation makes it possible to separate an objects implementation from its behavior to restrict access to its internal data. This restriction allows certain details of an objects behavior to be hidden. It allows us to create a “black box” and protects an object’s internal state from corruption by its clients.

The concept of data encapsulation is supported in C++ through the use of the public, protected and private keywords which are placed in the declaration of the class.

Anything in the class placed after the public keyword is accessible to all the users of the class. Elements placed after the protected keyword are accessible only to the methods of the class or classes derived from that class. Elements placed after the private keyword are accessible only to the methods of the class.

The above is a brief about Data Encapsulation compiled from various other websites. Keep watching this space for some of the trends in Technology.

Leave a Reply

Your email address will not be published. Required fields are marked *