Encapsulation In C#

INTRODUCTION

The object oriented programming will give the impression very unnatural to a programmer with a lot of procedural programming experience. In Object Oriented programming Encapsulation is the first pace. Encapsulation is the procedure of covering up of data and functions into a single unit (called class). An encapsulated object is often called an abstract data type. In this article let us see about it in a detailed manner.

NEED FOR ENCAPSULATION

The need of encapsulation is to protect or prevent the code (data) from accidental corruption due to the silly little errors that we are all prone to make. In Object oriented programming data is treated as a critical element in the program development and data is packed closely to the functions that operate on it and protects it from accidental modification from outside functions.

Encapsulation provides a way to protect data from accidental corruption. Rather than defining the data in the form of public, we can declare those fields as private. The Private data are manipulated indirectly by two ways. Let us see some example programs in C# to demonstrate Encapsulation by those two methods. The first method is using a pair of conventional accessor and mutator methods. Another one method is using a named property. Whatever be the method our aim is to use the data with out any damage or change.

ENCAPSULATION USING ACCESSORS AND MUTATORS

Let us see an example of Department class. To manipulate the data in that class (String departname) we define an accessor (get method) and mutator (set method).

  1. using system;  
  2. public class Department {  
  3.     private string departname;.......  
  4.     // Accessor.  
  5.     public string GetDepartname() {  
  6.         return departname;  
  7.     }  
  8.     // Mutator.  
  9.     public void SetDepartname(string a) {  
  10.         departname = a;  
  11.     }  
  12. }  

Like the above way we can protect the private data from the outside world. Here we use two separate methods to assign and get the required data.

  1. public static int Main(string[] args) {  
  2.     Department d = new Department();  
  3.     d.SetDepartname("ELECTRONICS");  
  4.     Console.WriteLine("The Department is :" + d.GetDepartname());  
  5.     return 0;  
  6. }  

 

In the above example we can't access the private data departname from an object instance. We manipulate the data only using those two methods.

ENCAPSULATION USING PROPERTIES

Properties are a new language feature introduced with C#. Only a few languages support this property. Properties in C# helps in protect a field in a class by reading and writing to it. The first method itself is good but Encapsulation can be accomplished much smoother with properties.

Now let's see an example.

  1. using system;  
  2. public class Department {  
  3.     private string departname;  
  4.     public string Departname {  
  5.         get {  
  6.             return departname;  
  7.         }  
  8.         set {  
  9.             departname = value;  
  10.         }  
  11.     }  
  12. }  
  13. public class Departmentmain {  
  14.     public static int Main(string[] args) {  
  15.         Department d = new Department();  
  16.         d.departname = "Communication";  
  17.         Console.WriteLine("The Department is :{0}", d.Departname);  
  18.         return 0;  
  19.     }  
  20. }  

 

From the above example we see the usage of Encapsulation by using properties. The property has two accessor get and set. The get accessor returns the value of the some property field. The set accessor sets the value of the some property field with the contents of "value". Properties can be made read-only. This is accomplished by having only a get accessor in the property implementation.

READ ONLY PROPERTY

  1. using system;  
  2. public class ReadDepartment {  
  3.     private string departname;  
  4.     public ReadDepartment(string avalue) {  
  5.         departname = avalue;  
  6.     }  
  7.     public string Departname {  
  8.         get {  
  9.             return departname;  
  10.         }  
  11.     }  
  12. }  
  13. public class ReadDepartmain {  
  14.     public static int Main(string[] args) {  
  15.         ReadDepartment d = new ReadDepartment("COMPUTERSCIENCE");  
  16.         Console.WriteLine("The Department is: {0}", d.Departname);  
  17.         return 0;  
  18.     }  
  19. }  

 

In the above example we see how to implement a read-only property. The class ReadDepartment has a Departname property that only implements a get accessor. It leaves out the set accessor. This particular class has a constructor, which accepts a string parameter. The Main method of the ReadDepartmain class creates a new object named d. The instantiation of the d object uses the constructor of the ReadDepartment that takes a string parameter. Since the above program is read-only, we cannot set the value to the field departname and we only read or get the value of the data from the field. Properties can be made also Write-only. This is accomplished by having only a set accessor in the property implementation.

WRITE ONLY PROPERTY

  1. using system;  
  2. public class WriteDepartment {  
  3.     private string departname;  
  4.     public string Departname {  
  5.         set {  
  6.             departname = value;  
  7.             Console.WriteLine("The Department is :{0}", departname);  
  8.         }  
  9.     }  
  10. }  
  11. public class WriteDepartmain {  
  12.     public static int Main(string[] args) {  
  13.         WriteDepartment d = new WriteDepartment();  
  14.         d.departname = "COMPUTERSCIENCE";  
  15.         return 0;  
  16.     }  
  17. }  

 

In the above example we see how to implement a Write-only property. The class WriteDepartment has now has a Departname property that only implements a set accessor. It leaves out the get accessor. The set accessor method is varied a little by it prints the value of the departname after it is assigned.

CONCLUSION

The Encapsulation is the first footstep towards the object-oriented programming. This article gives you a little bit information about Encapsulation. Using accessor and mutator methods we can make encapsulation. Another one method is using a named property. The benefit of properties is that the users of your objects are able to manipulate the internal data point using a single named item.

Up Next
    Ebook Download
    View all
    Learn
    View all