Encapsulation in C#

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
public int Add(int a, int b)  
{  
      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 
using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Text;    
    
namespace BAL    
{    
    public class EncapsulationImpentation    
    {    
        public int Add(int a, int b)    
        {    
            return a+b;    
        }    
        private int  sub(int c, int d)    
        {      
            return c-d;    
        }    
        protected int div (int e,int f)    
        {    
            return e / f;              
        }      
    }    
}    

After creating the object only the add method is available.
EncapsulationImpentation obj = new EncapsulationImpentation ();  
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.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode