Encapsulation in C#

Background

In this article we will learn about one of the most reusable object oriented features of C#, Encapsulation. We will learn about Encapsulation from the basics because I have written this article focusing on the students and the beginners. Before proceeding further, please refer to my previous articles for a better understanding.

So, let us start from the basics of Encapsulation.

Encapsulation
 
Encapsulation is the process of binding data and code into a single unit (object), it is also the process of hiding the internal details of an object and showing only the information to the user that the user needs.

Encapsulation is implemented using access modifiers that help to protect data from being misused and also prevents access to private data. It hides the complexity.

Key Points

It wraps the code and data into a single unit called object.

Encapsulation is implemented using access modifiers.

Encapsulation is useful to hide the internal details of an object.

Syntax

  1. public int Add(int a, int b)  
  2. {  
  3.       return a+b;  

Let's us see what the types of access modifiers are quickly that are useful to implement.

There are the following types of access modifiers in C#:

  1. Public
  2. Private
  3. Protected 
  4. Internal
  5. Protected internal

Now I will introduce each access modifier briefly.

Public
 

Public is the most commonly used access specifier in C# . It can be accessrd from anywhere; there is no restriction on accessibility. The scope of the accessibility is inside the class as well as outside. The type or member can be accessed by any other code in the same assembly or another assembly that references it.

The members of a public access specifier can be accessed within the class in which they are declared.

Within the derived classes of that class something is available within the same assembly.

Outside the class within the same assembly.

Private

The scope of the accessibility is limited only inside the classes or struct in which they are declared. The private members cannot be accessed outside the class and it is the least permissive access level.

The members of a Private access specifier can be accessed only within the class in which they are declared.

Protected

The scope of accessibility is limited within the class or struct and the class derived from this class.

The members of protected access specifier can be accessed within the class in which they are declared, within the derived classes of that class available within the same assembly and within the derived classes of that class available outside the assembly.

Internal 

The internal access modifiers provide access within the program that contains its declarations and also access within the same assembly level but not from another assembly.

The members with an internal access specifier can be accessed within the class in which they are declared, within the derived classes of that class available within the same assembly and outside the class within the same assembly.

Protected Internal

Protected internal is the same access levels of both protected and internal. It can be accessed anywhere in the same assembly and in the same class also the classes inherited from the same class.

The members of protected internal access specifier can be accessed within the class in which they are declared, within the derived classes of that class available within the same assembly, outside the class within the same assembly and within the derived classes of that class available outside of the assembly.

I hope you understand about the access modifiers. Now let us implement Encapsulation using access modifiers as shown in the following example.

Example

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace BAL    
  7. {    
  8.     public class EncapsulationImpentation    
  9.     {    
  10.         public int Add(int a, int b)    
  11.         {    
  12.             return a+b;    
  13.         }    
  14.         private int  sub(int c, int d)    
  15.         {      
  16.             return c-d;    
  17.         }    
  18.         protected int div (int e,int f)    
  19.         {    
  20.             return e / f;              
  21.         }      
  22.     }    
  23. }   

After creating the object only the add method is available.

  1. EncapsulationImpentation obj = new EncapsulationImpentation ();  
  2. obj.add(10, 10); 

From the preceding example its clear that after creating an object only the public access specifier method is available, the others are not  available, in other words after creating the object other internal details of an object are hidden, so because of this we can maintain the security of the code that  only shows the data that is necessary.

Summary

I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me. 

Up Next
    Ebook Download
    View all
    Learn
    View all