# OOP. Encapsulation and data hiding

Home Forums Programming Software engineering Object-oriented programming OOP. Encapsulation and data hiding

This topic contains 0 replies, has 1 voice, and was last updated by  Васильев Владимир Сергеевич 3 months, 2 weeks ago.

• Author
Posts
• #4079

This article explaines the difference between two close termines used in object-oriented programming: data hiding and encapsulation.

Some people might say it’s an interesting topic to discuss, some might say it’s rather funny, and some might say there is nothing here to discuss. Programming, as other sciences, has its own terminology. Each term describes a certain phenomenon. And those who claim themselves experts in this field, have to know this terminology, whether they like it or not. Therefore, all the attacks and replicas like “Who needs those theory discussions, I’m a programmer, not a teacher,” can be left somewhere else. And if you really think so, you are not a professional. Cause professionals are very accurate and respectful towards what they are using as their professional tools. There is an appropriate phrase which I think can be used in such cases: “It’s a must“. So, here and now we shall talk about termines which many people do not consider to be very simple to understand. What is the difference between Encapsulation and Data Hiding.

In some way, I shall repeat things I have already explained before in the article about Encapsulation. But repeating is another way to learn something. So, let’s start with the Encapsulation.

Those who are familiar with procedural languages ​​such as C, know the problem of global variables. When data is separated from functions which are using them, thus data and logic are defined separately.

Or, for example, C ++, which, though considered to be an object-oriented programming language, yet allows the definition of variables that are stored outside of classes or structures. When I think of what it would look like in the real world, if the laws of encapsulation were violated for some reason, I see a grandmother sitting on the couch, and next to it on the bedside table – her false teeth, a wig, glasses and prosthetic left leg near the couch. A terrible view. Or, for example, I go to a restaurant, order a duck with cherry sauce, and the cook with a shining smile cuts off the duck’s head right in front of my eyes, guts it and throws it into boiling water. Not very enjoyable either.

In the first case – I would like more integrality of the object, in the second – less detail about the process.

This is something encapsulation and data hiding are based on. The details of implementation and the data are packed into an object, and the object is their owner.

On one hand, we don’t need to go into details as to how and what the object performs. On the other hand – it’s easier and safer to operate the object’s data.

Easier – because they are all placed within the object itself; safer – because like this it‘s possible to establish access rules to all of its data and functionality.

And the latter is exactly what is called data hiding.

Data hiding is achieved through access modifiers (in C# those are private, public, protected, protected internal), and each of them defines the type members’ availability.

Now let’s see an example. Here we define the class Person, which encapsulates two private fields – name and age. The modifier private in C# means that access to the field is only possible within the class itself, but not through an instance and not to the derived classes. Access control for reading and writing is provided by the public properties Name and Age. They are verifying assigned values ​​for the range and format.

class Person
{
private string name;
private int age;

public string Name
{
get {return name; }
set
{
if (value.Length> = 2
&& value.Length <= 25)
{
name =
value;
}
else
{

throw new FormatException ("the length of name can't be less than 2 or
longer than 25");
}
}
}

public int Age
{
get {return age; }
set
{
if (value> 1
&& value <120)
{

age = value;
}
else
{

throw new ArgumentOutOfRangeException ();
}
}

}

public override string ToString ()
{
return String.Format ("name - {0},
age - {1}", Name, Age);
}
}

class Program
{
static void Main ()
{
Person person = new Person () {Name
= "John", Age = 147};
Console.WriteLine (person);
}
}

We can say that hiding is one of the the tasks of encapsulation. But it’s not correct to say that encapsulation data hiding are the same thing. Neither it would be right to claim that Encapsulation means only data hiding. Because the aim of Encapsulation lies beyond the scope of data hiding.

If we say that some method encapsulates an algorithm, we mean that this method perfoms a certain calculation, the details of which we don’t have to know, but we can trust its result and can use it. Though we can claim that the object which encapsulates data can hide it – which means it can protect its data from unauthorized access.

Thus, it’s important to distinguish between the concept of encapsulation and data hiding and it’s necessary to put the line between them.

You must be logged in to reply to this topic.