What are Accessors(Properties)? Whats the power of Encapsulation?

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

  1. private (attributes and methods are private by default)
  2. public
  3. protected
  4. internal
  5. 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 methodsThere are two ways of providing access to private attributes through pubilic methods.

  1. Get/Set methods
  2. 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.

Author: azagappan

Hi.. I am Azagu. I live in Chennai, Tamil Nadu, India. I am a .NET developer. You can reach me at azagappan at gmail.com

One thought on “What are Accessors(Properties)? Whats the power of Encapsulation?”

Leave a comment