What is Access Specifiers

Access specifier defines the accessibility of class member. We can restrict and limit the accessibility of member of classes.

Access Specifier Table

		
	
Access Spicifier Within class Derived Class Outside Class Within Assembly
Public Yes Yes Yes Yes
Private Yes No No No
Protected Yes Yes No No
Internal Yes No No Yes
Protected Internal Yes Yes No Yes

Above table defines the accessibility of all access spicifier. As this table describe that public member can be accessed everywhere and private accessed only with in class. This creates the protection level that except the class member no other can access the private member of class.

The details of protected and internal are following:-

Protected

As I define in the table that protected member can only be accessed with in class and only derived class. This member cannot be access outside the class and derived class.

For example

Create class having method declared as protected

ProtBaseClass.cs

		
	using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class ProtBaseClass
{
    //create protected method
    protected int sum(int x, int y)
    {

        return x + y;
    }
}

		

Create the aspx page and make the object of this class in page load and try to access the protected member in it.


access specifier

		Figure 1

As we create the object of class ProtBaseClass and try to access the method but as it declared as Protected we are not able to access it outside the class.

Now create another class and derived ProtBaseClass in it. And try to access the protected member of class in it.

ProtDerivedClass.cs


		using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class ProtDerivedClass:ProtBaseClass
{
    public int mul()
    {
       // access the protected method in derived class.
       int p= sum(5,6);
       return p;
    
    }
}
	

As we seen that protected member can be accessed only in the class and in derived class.

Internal

Internal class members are those members which only accessed in the class and in a same assembly. Before understand the internal we have to understand what the assembly is? Assembly is file which contains all the deployment detail of a program. It is the executable file having extension .dll.

Internal class’s member can access in a same class and same assembly.

For Example

Create a class and declare member as internal

InternalClass.cs


		using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class InternalClass
{
    //create method as internal
    internal int sum(int x, int y)
    {

        return x + y;
    }
}


	

Create aspx page and create the object of InternalClass and try to access the internal method


internal access specifier

		Figure 2

As we seen we are not able to access the method sum declared internal in the class because it can only access in a class and same assembly. Now create another class and try to access the method in the class.

InternalAccessClass.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class InternalAccessClass
{
    public int mul()
    {
        // create the object of internal class
        InternalClass ic = new InternalClass();
        //access the method sum which is declared as internal
       int p= ic.sum(5, 4);
       return p;
    
    }
}

In this class we are able to access the internal member as it comes in same assembly.

I know you are thinking how?

All classes created in app_code and when we create the assembly of it (publish the project for deployment) it comes under the same assembly.

Note:-only one assembly created for the app_code.

If we create the class and do and add it in the app_code then we cannot access the internal member in it as it does not come under the same assembly.

For Example


internal access specifier output

		Figure 3

This class is not created in app_code so we are not able to access the internal member of class.

Protected Internal

Protected internal is hybrid of protected and internal. We can access these member in protected and in same assembly too.