Why to use Encapsulation?
In this article I will explain the concept of Encapsulation and how to implement it with the help of properties. It also sheds light on Why, When and How to use a property. Further It provides detailed explanation for creating a property with accessor and mutator methods and making a property read-only or write-only or read write both.
Encapsulation
Definition: The wrapping up of data and functions into a single unit (called class) is known as Encapsulation.Introduction
All the programmers familiar to the object oriented programming world knows the above mentioned definition of Encapsulation but very few are aware about it's use and how to implement it in a project. Those who implement most of the time does not know that what they are doing is a technique of encapsulation.Getting Started
Data Encapsulation is the most striking feature of a class. The data is not accessible to outside world and only those functions which are wrapped in the class can access it.
The function provides the interface between objects data and the program. This insulation of data from direct access by the program is called Data hiding or Information Hiding. This is implemented using the mutator and accessor methods of a property.Example
Till here we went through the theoretical explanation. But the question is How, When and Why to use it in our projects?
To understand this consider a class A with a private string variable Author as follows.
class A
{
private string Author;
public A()
{
Author = "Sibtain";
}
}
Consider Another class B where I want to access the value of this variable Author. I'll make object of class A and will access the value. But the variable Author is not available for access because it is private.
Now the option available is to make the author variable public which will allow it to be accessed form class B but this is a wrong technique.
* What if I don't want the value of Author to be changed through an object of class A ?
* What if I don't want to show the current value of Author but want to allow it to be changed thorugh an object of class A ?
* What if I want to allow the users to see the current value but not to change throug the object of class A?
If I'll make the variable public then I will not be able to achieve this. The value can be accessed and can also be changed through the class' object.
The solution is in the formation of a property for Author variable. Hence the access to a variable's value should always be through a property.
In C#, properties are natural extension of data fields. They are usually known as "smart fields". Why to use a property?
Use of property for a variable of a class gives you control over the way the value of the variable to be accessed.
A property encapsulates a variable of a class and defines the mode of access i.e. Read-Only, Write-Only or Both.
To understand the above statement add the following code in class A which will create property for variable Author.
public string AuthorName
{
get { return Author; }
set { Author = value; }
}
The name of the property is AuthorName and it encapsulates the private variable Author. It contains the Accessor Method
get { return Author; }
and mutator method
set { Author = value; }
Now if you want to access the value of variable Author from class B, you can access it through this property.
We can access as well as modify or change the value of the Author variable through the property as follows.Making a property Read-Only
Using the property we can control what the user can do with the value of the variable.
For example when someone creates the object of class A, then I want the value of the Author variable to be available for accession but it should not be changed or modified through the object of class A. In other words I want to make the author variable Read-Only.
This can be achieved by tailoring the AuthorName property as follows.
public string AuthorName
{
get { return Author; }
}
Now the user (object of class A created in class B) can get the value of Author but will not be able to set it.Making a property Write-Only
Next consider the requirement as vice versa. I want to allow any object of class A to change the value of author variable but no one should be able to see what it's current value. In other words I want to make the variable author Write-Only. For this tailor the AuthorName property as follows.
public string AuthorName
{
set { Author = value; }
}Conclusion
We have learnt the very important concept of encapsulation with its practical implementation using properties.
We have also seen how the mutator and accessor methods of the property give full control over the way the value of the variable of a class to be accessed.
And finally we learned how to tailor the properties in order to make them read-only and write-only.
I hope that this article will add value to your knowledge and you like the simple and clean explanation.
(Don't forget to rate the content and leave your responses.)
Thanks 'n Regards,
Sibtain Masih
Hi Sibtain,
Thanks a lot for sharing an interesting, already known topic with a brief explanation,
Its easy to understand.
I have a little doubt that if i m having two objects viz objA and objB of the class A and using objA i have changed value as "Tony", now if i try to access the value of objB.AuthorName, ll it return the default value 'Sibtain' or changed value 'Tony'?, (since both the object ll point to the same memory), Help me out to clear the doubt, thanks in advance.