Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#

Access Modifiers for Beginners

Rate me:
Please Sign up or sign in to vote.
4.60/5 (32 votes)
6 Aug 2013CPOL5 min read 39.7K   47   19
Understand access modifiers before using them.

Introduction

This article is for beginners who have just started programming. Here I will show you how access modifiers help us design a class and when and why we use which modifier.

Background

If you search for the words "access modifiers" on the internet, you will find lots of references. I did the same and I am showing the below from MSDN.

  • public: Access is not restricted.
  • private: Access is limited to the containing type.
  • Internal: Access is limited to the current assembly.
  • protected: Access is limited to the containing class or types derived from the containing class.
  • protected internal: Access is limited to the current assembly or types derived from the containing class.

Using the Code

So let's start step by step by using a case study.

  • Case: Design a Customer class that will have id,
    name
    , reward-point variables. Id will be a random number; for general customer the reward point will always be 500, and for VIP customer it will be 1500. Also a print method is needed for each customer type which will show the output of the customer.
  • Analysis: So from the requirement, if we separate things, then we find the work to do is:
  1. Customer class with {private Id, public: Name, protected : RewardPoint}
  2. GenaralCustomer and VIPCustomer which inherits Customer Class
  3. Client for each customer type class which gives the name of the customer and calls the print method.

The below code is for analysis #1:

C#
namespace SameAssembly
{
    public class Customer
    {
        private int Id {get;set;} /*private : Access is limited to the containing type.*/
        public string Name {get;set;}/*public : Access is not restricted.*/
        protected int RewardPoint{get;set;}/*protected : 
        			*Access is limited to the containing class 
                    * or types derived from the containing class.*/
        public Customer()
        {
            Id = new Random().Next();
        }

        /*internal :Access is limited to the current assembly or types. */
        internal virtual void PrintCustomerInfo()  
        {
            Console.WriteLine("\t\t\t\t ID:{0} \n\n \t\t\t\t 
            Name:{1} \n\n \t\t\t\t RewardPoint:{2} \n\n", Id, Name, RewardPoint);
        }
    }
}

The below code is for analysis #2 for GeneralCustomer:

C#
namespace SameAssembly
{
    public class GeneralCustomer : Customer
    {
        internal string CustomerType
        {
            get { return "******** FOR GENERAL CUSTOMER ****************"; }
        }
        public GeneralCustomer()
        {
            RewardPoint = 500;
        }

        internal override void PrintCustomerInfo()
        {
            Console.WriteLine("\n\n\t\t\t" + CustomerType + "\n\n\t\t\t");
            base.PrintCustomerInfo();
        }
    }
} 

The below code is for analysis #3 client for GeneralCustomer:

C#
namespace SameAssembly
{
    public class ClientForGeneral
    {
        public void Print()
        {
            var generalCustomer = new GeneralCustomer();
            generalCustomer.Name = "Mr Jhon";
            generalCustomer.PrintCustomerInfo();
        }
    }
}

If you look at the above Customer class, you see that ID is private. Why? Because we will not allow any other class to access it and also not even from the instance of the Customer class, we can only access it from the body of the Customer class where it is declared. So we assign it in the constructor of the Customer class. In short, we say "Access is limited to the containing type".

Name is public. Why? Because we allow access from everywhere. It can be accessed from a derived type or by an instance of Customer in the same assembly or a different assembly and also from the same content type. in short, we say "Access is not restricted".

Rewardpoint is protected. Why? Because we allow the access from its own containing class and also from the derive type in any assembly but not from the instance of base or derive type. One word we say "Access is limited to the containing class * or types derived from the containing class.";

PrintCustomerInfo() is internal. Why? Because we allow the access in the same assembly so that any class that is in the same assembly as the Customer class that inherits Customer will be able to override the method. In short, we say "Access is limited to the current assembly".

What does same assembly and different assembly mean?

Look at the picture below. Here my Customer class and Generalcustomer class are in the same assembly. Both the classes are in the same Class Library named SameAssembly and also share the same namespace. So we can say they are in the same assembly. ClientForVip and VipCustomer are in the same namespace/library. If Customer class is used in the VipCustomer class, then we can say that it is using different assemblies.

Image 1

Image 2

Notice both the members of the Customer class rewardpoint and PrintCustomerInfo can easily be accessed from the GeneralCustomer class. rewardPoint is protected so it can easily be accessed from the derived type and PrintCustomerInfo is internal so GeneralCustomer can access it because it is in the same assembly.

The below code is for analysis #2 for VIPlCustomer:

The code is:

C#
namespace DifferentAssembly
{
    public class VipCustomer : Customer
    {
        internal string CustomerType
        {
            get
            {
                return "******** FOR VIP CUSTOMER ****************";
            }
        }
        public VipCustomer()
        {
            RewardPoint = 1500;
        }
        internal  override void PrintCustomerInfo()
        {
            Console.WriteLine("\n\n\t\t\t" + CustomerType+ "\n\n\t\t\t");
            base.PrintCustomerInfo();
        }        
    }
}

Here is the trick. I place the VIPCustomer class in a different class library than the Customer class. From there, I want to access my Customer class members in the derived class VIPCustomer. So will the above code compile? No, it will give you the below compiled error:

Error 1 'DifferentAssembly.VipCustomer.PrintCustomerInfo()': 
no suitable method found to override 
J:\Access Modifiers\AccessModifiers\DifferentAssembly\VipCustomer.cs 18 33 DifferentAssembly 

Why? Because rewardpoint is OK, accessible from any derived type as it is protected. But PrintCustomerInfo can't access from another assembly as it is defined as internal. As I said earlier:

PrintCustomerInfo() is internal. Why? Because we allow the access in the same assembly, so that any class that is in the same assembly as the customer class that inherits customer class will be able to override the method.

So what's the solution? Will we make it protected, does it solve our issue? No, not at all. It will raise another issue on our client class ClientForGeneral because we can't access PrintCustomromerInfo from there if it's protected. The below line will give an error.

C#
generalCustomer.PrintCustomerInfo();  

So what's the solution? The solution is protected internal. We need to change the PrntCustomerInfo method of the Customer class like below:

C#
/*protected internal :Access is limited to the current assembly or types 
 * derived from the containing class. */
protected internal virtual void PrintCustomerInfo()  
{
    Console.WriteLine("\t\t\t\t ID:{0} \n\n \t\t\t\t 
    Name:{1} \n\n \t\t\t\t RewardPoint:{2} \n\n", Id, Name, RewardPoint);
}

In the current assembly, the client ClientForGeneral will have no problem at all to access PrintCustomerInfo through the object because it's in the same assembly; here it will act as internal.

So the override method in GeneralCustomerClass will be like below:

C#
protected  internal override void PrintCustomerInfo()
{
    Console.WriteLine("\n\n\t\t\t" + CustomerType+ "\n\n\t\t\t");
    base.PrintCustomerInfo();
}

In <code>VipCustomer, it will act as protected like below. Here you can't use protected internal; only protected modifier is valid here.

C#
protected  override void PrintCustomerInfo()
{
    Console.WriteLine("\n\n\t\t\t" + CustomerType+ "\n\n\t\t\t");
    base.PrintCustomerInfo();
} 

If you write it protected internal, you will receive the compile error:

Error 1 'DifferentAssembly.VipCustomer.PrintCustomerInfo()': cannot change access
 modifiers when overriding 'protected' inherited member
 'SameAssembly.Customer.PrintCustomerInfo()' 
J:\Access Modifiers\AccessModifiers\DifferentAssembly\VipCustomer.cs 17 42 DifferentAssembly

So the compiler will easily assume the derived type, it will act as protected not protected internal.

Points of Interest

What about the client class ClientForVip? How it will access the

PrintCustomerInfo()
method as it is now protected? We need to give some extra effort, we need another method that is accessible through that class instance.

C#
internal void PrintVipCustomerInfo()
{
    PrintCustomerInfo();
} 

Now the client class can access PrintCustomerInfo through its internal method PrintVipCustomerInfo.

C#
namespace DifferentAssembly
{
    class ClientForVip
    {
        public void Print()
        {
            var generalCustomer = new VipCustomer();
            generalCustomer.Name= "Mr Mark";
            generalCustomer.PrintVipCustomerInfo();
        }
    }
} 

If we add the reference of SameAssembly and DifferentAssembly to any console application and call the client of each type like below:

C#
namespace AccessModifiers
{
    class Program
    {
        static void Main(string[] args)
        {
            var generalClient = new ClientForGeneral();
            generalClient.Print();
            
            var vipClient = new ClientForVip();            
            vipClient.Print();
            Console.ReadKey();
        }
    }
}

It gives the following output:

Image 3

I found most of developers confused about the use of internal and protected internal. Hope this article helps them to clear their understanding about Access Modifiers.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Bangladesh Bangladesh
I am a Sr.Software Engineer at Brain Station -23. I have 5+ years of work experience in .Net technology. I strongly believe that before software can be reusable it first has to be usable.

My contact info :

mfrony2003@yahoo.com
mfrony2003@hotmail.com

LinkedIn
http://www.linkedin.com/profile/view?id=106671466&trk=tab_pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
johannesnestler25-Apr-14 0:54
johannesnestler25-Apr-14 0:54 
GeneralRe: My vote of 5 Pin
Faisal(mfrony)25-Apr-14 2:53
Faisal(mfrony)25-Apr-14 2:53 
Thanks
GeneralMy vote of 3 Pin
Jasmine250112-Aug-13 7:33
Jasmine250112-Aug-13 7:33 
GeneralRe: My vote of 3 Pin
Faisal(mfrony)12-Aug-13 18:49
Faisal(mfrony)12-Aug-13 18:49 
GeneralRe: My vote of 3 Pin
Jasmine250113-Aug-13 5:20
Jasmine250113-Aug-13 5:20 
GeneralRe: My vote of 3 Pin
Faisal(mfrony)13-Aug-13 21:51
Faisal(mfrony)13-Aug-13 21:51 
GeneralMy vote of 5 Pin
Ehtesam Ahmed29-Jul-13 20:47
professionalEhtesam Ahmed29-Jul-13 20:47 
GeneralMy vote of 5 Pin
hopeful4ever26-Jul-13 9:53
hopeful4ever26-Jul-13 9:53 
GeneralRe: My vote of 5 Pin
Faisal(mfrony)26-Jul-13 16:55
Faisal(mfrony)26-Jul-13 16:55 
GeneralMy vote of 5 Pin
Southmountain26-Jul-13 8:18
Southmountain26-Jul-13 8:18 
GeneralMy vote of 4 Pin
Pravin Patil, Mumbai26-Jul-13 3:53
Pravin Patil, Mumbai26-Jul-13 3:53 
GeneralMy vote of 4 Pin
Antariksh Verma26-Jul-13 1:08
professionalAntariksh Verma26-Jul-13 1:08 
GeneralMy vote of 5 Pin
z53122-Jul-13 11:34
professionalz53122-Jul-13 11:34 
Question[My vote of 1] Comment Pin
FatCatProgrammer22-Jul-13 10:37
FatCatProgrammer22-Jul-13 10:37 
AnswerRe: [My vote of 1] Comment Pin
z53122-Jul-13 11:33
professionalz53122-Jul-13 11:33 
AnswerRe: [My vote of 1] Comment PinPopular
darkelflemurian22-Jul-13 12:32
darkelflemurian22-Jul-13 12:32 
GeneralMy vote of 4 Pin
Jasmine250122-Jul-13 8:33
Jasmine250122-Jul-13 8:33 
GeneralGood for beginners - vote 5 Pin
sund7wells22-Jul-13 0:37
sund7wells22-Jul-13 0:37 
GeneralMy vote of 5 Pin
Mati Rehman21-Jul-13 7:55
Mati Rehman21-Jul-13 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.