What is 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.

Leave a Reply

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