C# Tutorial - Encapsulation and Data Hiding

posted Jun 28, 2011, 7:13 PM by magic stone

Encapsulation

The idea of encapsulation comes from the need to cleanly distinguish between the specification and the implementation of an operation and the need for modularity. Modularity is necessary to structure complex applications designed and implemented by a team of programmers.

Encapsulation is a way of binding information and the behaviour performed upon the information together in a single object. This allows the user of the object, to see and use the interface of the object without needing to know the internal workings of the object.

The idea of encapsulation in programming languages comes from abstract data types. In this view, an object has an interface part and an implementation part. The interface part is the specification of the set of operations that can be performed on the object and is the only visible part of the object.

In encapsulation, there is a single model for data (information) and operations (behaviour), and information can be hidden. No operations, outside those specified in the interface, can be performed.

Encapsulation provides a form of "logical data independence": we can change the implementation of an object without changing any of the programs using the object. Encapsulation is obtained when only the operations are visible and the data and the implementation of the operations are hidden within the object.

An Example

In our everyday lives, most of us use a video recorder via either the controls on the front of it or via a remote control. We all know which buttons to press in order to watch a program or record a program, this is done via the interface we have with the video recorder. The manufacturer can change the internal workings of the hardware, but this would not often affect us as a user because the interface we have would be the same. That is a play button, a record button, fast forward, rewind, stop, pause, etc.

The VCR is encapsulated as a single object, which may change internally, but stays the same from the users interface point of view.

Coding Encapsulation

The following code demonstrates a class containing some data and a method that acts upon the data (without data hiding):

class Counter
{
  public int Count;

  public Counter()
  {
    Count = 0;
  }

  public void incCount()
  {
    Count++;
  }
}

Although the member variable can be directly accessed, the data and its method is encapsulated in a class.

Data Hiding

Data hiding is linked to encapsulation; however data hiding is not encapsulation as it is sometimes described in this way.

Data hiding is simply the means to remove direct access to an object’s information, by providing operations that perform actions on the data. This way any changes to the value of the data must come through the interface to the data, which is an operation. Thus we use access operations or properties.

An Example

In our person object, access to the data forename is supplied through the access operations set forename() and get forename().

Coding Data Hiding

The following code demonstrates a class containing some data that is hidden:

class Counter
{
  private int Count;

  public Counter()
  {
    Count = 0;
  }

  public void setCount( int newVal )
  {
    Count = newVal;
  }

  public int getCount()
  {
    return Count;
  }
}

This piece of code is also encapsulated, showing that you can have encapsulation without data hiding, but you cannot have data hiding without encapsulation.

Comments