Encapsulation

Encapsulation is defined ‘as the process of enclosing one or more items within a physical or logical package’. Encapsulation, in object-oriented programming methodology, prevents access to implementation details.

Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member.

Access modifiers are the defined level of permission to access properties and methods. By declaring these access modifiers, we are defining a variable or an event can be accessed from assembly to within that class. Let’s see how.

 

C# Access Modifiers / Specifiers

There are 6 major access modifiers in C#. These are:

  1. Public
  2. Private
  3. Protected
  4. Internal
  5. Protected Internal
  6. private protected

 

visibility for C# classes and member
  • Public: Anywhere from the class
  • Private: Only within the class
  • Protected: Only within that class and the sub classes of that class
  • Internal: Within the assembly of the class
  • Protected Internal: Within that class, sub classes of that class and and assembly
  • private protected: within that class or struct, or in a derived class from the same assembly, but not from another assembly

 

Default visibility for C# classes and members if not specified

Public

Using Public, an event or a variable can be accessed from outside of the class, where it belongs. And also from the outside of the assembly.

class ClassTest
{
    //Public method
    public void MethodPublic()
    {
        // defination of MethodPublic
    }
}

// to access the method
class Program
{
    static void Main(string[] args)
    {
        ClassTest objClassTest = new ClassTest();

        objClassTest.MethodPublic(); // valid code to access.
    }
}

Private

It restricts the use of methods and variables only within the class itself. It can’t be used from outside of the class. As you declare a private constructor of a class, that class can’t be accessed from outside that class, you can’t create an object of that class,

Example 1: Private keyword

class ClassTest
{
    //Private method
    private void MethodPrivate()
    {
        // defination of MethodPrivate
    }
}
// to access the method
class Program
{
    static void Main(string[] args)
    {
        ClassTest objClassTest = new ClassTest();

        objClassTest.MethodPrivate(); // invalid code to access.
    }
}

 

Example 2: Private Constructor

class ClassTest
{
    private ClassTest() { } // private constructor
}
// to access the method
class Program
{
    static void Main(string[] args)
    {
        // invalid code. can't create an object of this class
        ClassTest objClassTest = new ClassTest();
    }
}

Protected

This allows variables and methods to access from that class and the sub class of the class. That means that methods can be accessed within that class and from the classes, which actually inherit that class.

class ClassTest
{
    //Protected variable
    protected int _a;
}

class ClassTest2 : ClassTest
{
    ClassTest2()
    {
        this._a = 10; // can access from this class
    }
}

class ClassTest3
{
    ClassTest3()
    {
        this._a = 10; // can't access from this class
    }
}

Internal

Internal is introduced in C#. In JAVA, we don’t have this access modifier. This allows the access after Protected. As Protected, it also allows to access the methods and variables from that class and the sub classes of that class. It added the assembly into it. That means the variables and methods can be accessed within the assembly where the class belongs. Now make sure that here we are talking about Namespace, because Namespaceand assembly are slightly different. An assembly can hold more than one Namespace. Assemblies are actually the DLL of the project.

class ClassTest
{
    internal void MethodInternal()
    {
        // do your code
    }
}
// to access the method
class Program
{
    static void Main(string[] args)
    {
        ClassTest objClassTest = new ClassTest();

        objClassTest.MethodInternal(); // valid code to access.
    }
}

Protected Internal

Protected Internal allows you to access the variables and methods to access from that class and sub classes of that class. Also allows to access within the same assembly. This means in protected, if the class is inheriting the super class and the method or variable is protected, then the assembly doesn’t matter to access. But in the Internal, the assembly matters if the class is inheriting the super class. That is why we use Protected Internalaccess modifier.

class ClassTest
{
    protected internal string name; // protected internal
    public void print()
    {
        Console.WriteLine("\nMy name is " + name);
    }
}
// to access the method
class Program
{
    static void Main(string[] args)
    {
        ClassTest objClassTest = new ClassTest();
        // Accepting value in protected internal variable
        objClassTest.name = "Arka";
        objClassTest.print();
    }
}