As discussed in the previous posts, attributes are the information we gather about an object. In real time projects, we often restrict the access to these attributes for variety of reasons. One such reasons is data security ( we are not going to get into details of this). These kinds of restrictions are called as Information Hiding
In a well designed object oriented model, an object publicizes what it can do, ie what services it can provide. It will hide the internal details like how its implemented, how it performs the service etc.
In OO terminology, this is achieved through the concept called Accessibility. Accessibility determines if the attribute/method is accessible outside of the class. In C#, the accessibility is achived by using the access modifier keyword at the begining of the declaration.
ex private int age; public string Name; public string GetFullName(); etc.
C# defines five tyes of accessibility. We will only list what they are. We will discuss them in future
The accees modifier keywords in C# are
- private (attributes and methods are private by default)
- public
- protected
- internal
- protected internal
Methods of a class are typically declared public (there can also be private methods in a class). The reason is that the class should publicize its service. On the other hand, the attributes of a calss are declared private (so that the object will have control over data) and give access to private attributes through public methods. There are two ways of providing access to private attributes through pubilic methods.
- Get/Set methods
- Properties. C# prefers to go with properties way. A property defines whats called as get accessor and set accessor. Note that get and set accessors are not methods.
Get/Set are public methods that access the private variable.
Properites or get/set methods are called as Accessors. ie, they are the methods that give access to objects private members.
Example
public int GetAge(){
return age;
}
public int SetAge(int myAge){
age = myAge;
}
Property in C# behaves like accessing the public attributes.
Example
public int Age{
set {
age = value; // note that value is implicitly declared variable.
}
get{
return age;
}
}
In client code, we will access like
<objectName>.Age = 12;
Console.WriteLine(<objectName>.Age);
The power of Encapsulation
We sofar have discussed that letting attribute values to be accessed through public method is the best way of implementation. It has the following advantages
- Authorizied access to attributes – Some attributes are only accessible by the object itself, some are accessible to outside world
- Data Integrity. you can have logics in get/set method that prevent aribitary setting of attributes. For example, if you have a date attribute inside your class, you can have logic inside get/set methods, that prevents any other values other than date to be set to the attribute
- Hiding the implementation details. Your method implenetation details are always hidden from the users of the service. This means that you can change the method logic without affecting the users of the service.
Useful info. Thanks.