Understanding Protected, Internal and Protected Internal Access modifiers in OOPs

Kishork80
Posted by in OOPS category on for Beginner level | Views : 51765 red flag
Rating: 5 out of 5  
 3 vote(s)

Here we will see and understand OOP's very basic but difficult to show concept of Protected, Internal and Protected Internal Access modifiers in OOPs using C# code examle.

Introduction

We all know access modifiers are said to be ‘Back-bone of OOPs ‘.Which seems to be very easy to remember (for interview purposes) but at the same time much difficult to show and understand by an example to someone when asked by anyone (at least for me J)

I digged-into it one fine Saturday morning and felt very happy to understand  all of them. Here I tried to show the behavior of three most important but ‘confusing’ Access Modifiers in C# .

1.       Protected(P)

2.       Internal(I)

3.       Protected Internal(PI)

The 'Concept'

First of all Let us remember the exact definitions of them as per various sources.

1.       Protected: The type or member can only be accessed by code in the same class or struct, or in a derived class.

2.       Internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

3.      Protected Internal:  The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

Quite easy .. Right! Yes, of course.

Understanding and coding

Let us take an example in C#. Please refer the below code at the same time.

·         Create a Console Application/ Project in C#.

·         Create Two Separate assemblies named like ‘Assembly_1’ and ‘Assembly_2’.

·         Now we will write code in the first assembly i.e. ‘Assembly_1’.

·         Write two normal Class named ‘Soul’ and ‘Soul_1’.

·         Now inside class ‘Soul’ , create four different classes ‘A’ ,’B’,’C’ and ‘G’. Here classes ‘A’ ,’B’ and ’C’ contains different access-modifiers! Noticed!!! But not class ‘G’ which is by default internal.

·         Write some static public functions like fnA(), fnB()’ ,‘fnC()’ and ‘fnG()’ having some simple output messageslike below in the code.

·         Now try calling all there functions one-by-one inside class ‘G’ like

                A.fnA();

                B.fnB();

                C.fnC();

                Console.WriteLine("fnG");

·         What do we find??? The class ‘G’ is inside the same class 'Soul' so everything is accessible from above. Does not consider a modified indeed. Hurray!!

·         Now the Next step. Try calling the same code inside class ‘Soul_1’.

·         What do we find??? This class is NOT in same class 'Soul' but in the same assembly ‘Assembly_1’ so  only protected is NOT accessible rest all are accessible. If you want to access protected then need to inherit class 'Soul'.

·         Wow that was great!

·         Now we will move to another assembly named ‘Assembly_2’.

·         Write a class ‘KISH’ and inherit previous class Soul from Assembly_1 like

 

 class KISH : Assembly_1.Soul

{…}

·         Last but not the least create a class ‘D’ and call the above methods

       Console.WriteLine("fnD");

             A.fnA();

             B.fnB();

             C.fnC();

What do we find??? If it does not inherit class 'Assembly_1.Soul' then we cannot access anything because this is 'ANOTHER' assembly .  Hence, only INTERNAL is NOT accessible , rest all are accessible from first assembly if it inherits class 'Soul'.

Please refer the code below:

namespace Assembly_1 //This is first assembly.

{

    //A normal public class which contains all different types of access-modifier classes in the assembly named 'Assembly_1'

    public class Soul              

    {

        //protected class A

        protected class A

        {

            public static void fnA()

            {

                Console.WriteLine("fnA");

            }

        }

       

        //internal class

        internal class B

        {

            public static void fnB()

            {

                Console.WriteLine("fnB");

            }

        }

 

        //protected internal

        protected internal class C

        {

            public static void fnC()

            {

                Console.WriteLine("fnC");

            }

        }

 

        //TIP 1:This class is inside the same class 'Soul' so everything is accessible from above.Hurray!!

        class G                       

        {

            public static void fnG()

            {

                //All methods are easily accessible.Does not consider a modified indeed.

                A.fnA();

                B.fnB();

                C.fnC();

                Console.WriteLine("fnG");

            }

        }

    }

 

    //Different class but inside the same assembly named 'Assembly_1'

    public class Soul_1

    {

        //Accesibility of 'protected members'::NO

        //Accesibility of 'internal members'::YES

        //Accesibility of 'protected internal' members::YES

        //TIP 2:This class is NOT in same class 'Soul' but in the same assembly so  only protected is NOT accessible rest all are accessible.

        //IF you want to access protected then inherit class 'Soul'.

        public void fnSoul_1()

        {

            //Soul.A.fnA();//ERROR:Accesibility of 'protected'::NO.ONLY WHEN Soul_1:Soul i.e. when  Soul_1 inherits Soul.

            Soul.B.fnB();

            Soul.C.fnC();

            Console.WriteLine("fnSoul_1");

        }

    }

}

 

namespace Assembly_2 //This is second assembly.

{

    //TIP:if it does not inherit class 'Soul' then we can not access any thing beacuse this 'ANOTHER' assembly.

    //TIP:only INTERNAL is NOT accessible , rest all are accessible from first assembly if it inherits class 'Soul'

    class KISH : Assembly_1.Soul

    {

        //=======================================================================

        class D

        {

            void fnD()

            {

                //

                Console.WriteLine("fnD");

                A.fnA();//YES, becuase this is 'protected'

                //B.fnB();//ERROR:NO, becuase this is 'internal':TIPS 3:only internal is not accessible.

                C.fnC();//YES, becuase this is 'protected internal'

            }

        }

    }

}


 

Conclusion

Here is a Short and simple Matrix to memorise the above concept.

Accessbility Matix to Remember Same Assembly Different Assembly
Access Modifer Type Short Name Same Class Different Class   Any class(inherits previous assembly class)
Protected P Yes No   Yes
Internal I Yes Yes   No
Protected Internal PI Yes Yes   Yes



Hope it will help you to crack OOP's concept .Cheers!!

-Kishor Kumar

Page copy protected against web site content infringement by Copyscape

About the Author

Kishork80
Full Name: kishor kumar
Member Level: Starter
Member Status: Member
Member Since: 7/1/2010 2:49:48 AM
Country: India
kishor kumar
http://www.dotnetfunda.com
Having 6 pyears of exp in dot net and still counting...

Login to vote for this post.

Comments or Responses

Posted by: Sabarimahesh on: 3/16/2012 | Points: 25
Hi kishor kumar

I Got A Very Basic Knowledge about This Access Modifiers

Thnks YOu
Posted by: Sabarimahesh on: 3/16/2012 | Points: 25
I Used ur Code Kish...

See My Below code:


using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
public class Soul
{
//protected class A
protected class A
{
public static void fnA()
{
Console.WriteLine("fnA");
}
}

//internal class
internal class B
{
public static void fnB()
{
Console.WriteLine("fnB");
}
}

//protected internal
protected internal class C
{
public static void fnC()
{
Console.WriteLine("fnC");
}
}

//TIP 1:This class is inside the same class 'Soul' so everything is accessible from above.Hurray!!
class G
{
public static void fnG()
{
//All methods are easily accessible.Does not consider a modified indeed.
A.fnA();
B.fnB();
C.fnC();
Console.WriteLine("fnG");
}
}
}
}
namespace ConsoleApplication31
{
class KISH : ConsoleApplication3.Soul
{
//=======================================================================
class D
{
void fnD()
{
//
Console.WriteLine("fnD");
A.fnA();//YES, becuase this is 'protected'
//B.fnB();//ERROR:NO, becuase this is 'internal':TIPS 3:only internal is not accessible.
C.fnC();//YES, becuase this is 'protected internal'
}
}
}
}


I got A Error
:Error Program 'E:\Documents and Settings\Administrator\Desktop\ConsoleApplication3\ConsoleApplication3\obj\Debug\ConsoleApplication3.exe' does not contain a static 'Main' method suitable for an entry point ConsoleApplication3

My Doubt
: Can I want To Delete That Static Void Main Function



Posted by: Kishork80 on: 3/16/2012 | Points: 25
This is assembly. Pleaz call the functions inside a main function
Posted by: Sudhakar162002 on: 3/17/2012 | Points: 25
Thanks for the post having good explanation and example for the beginners like me.keep posting this type of articles.
Posted by: Arun_Ch on: 3/19/2012 | Points: 25
Hi Kishor,
I'm Work out Ur Code it is fabulous thing thanks.....
Posted by: Hafeef on: 7/24/2012 | Points: 25


namespace Assembly_1 //This is first assembly.

{

//A normal public class which contains all different types of access-modifier classes in the assembly named 'Assembly_1'

public class Soul

{

//protected class A

protected class A

{

public static void fnA()

{

Console.WriteLine("fnA");

}

}



//internal class

internal class B

{

public static void fnB()

{

Console.WriteLine("fnB");

}

}



//protected internal

protected internal class C

{

public static void fnC()

{

Console.WriteLine("fnC");

}

}



//TIP 1:This class is inside the same class 'Soul' so everything is accessible from above.Hurray!!

class G

{

public static void fnG()

{

//All methods are easily accessible.Does not consider a modified indeed.

A.fnA();

B.fnB();

C.fnC();

Console.WriteLine("fnG");

}

}

}



//Different class but inside the same assembly named 'Assembly_1'

public class Soul_1

{

//Accesibility of 'protected members'::NO

//Accesibility of 'internal members'::YES

//Accesibility of 'protected internal' members::YES

//TIP 2:This class is NOT in same class 'Soul' but in the same assembly so only protected is NOT accessible rest all are accessible.

//IF you want to access protected then inherit class 'Soul'.

public void fnSoul_1()

{

//Soul.A.fnA();//ERROR:Accesibility of 'protected'::NO.ONLY WHEN Soul_1:Soul i.e. when Soul_1 inherits Soul.

Soul.B.fnB();

Soul.C.fnC();

Console.WriteLine("fnSoul_1");

}

}

}



namespace Assembly_2 //This is second assembly.

{

//TIP:if it does not inherit class 'Soul' then we can not access any thing beacuse this 'ANOTHER' assembly.

//TIP:only INTERNAL is NOT accessible , rest all are accessible from first assembly if it inherits class 'Soul'

class KISH : Assembly_1.Soul

{

//=======================================================================

class D

{
static void Main()
{
A.fnA();//YES, becuase this is 'protected'

B.fnB();//ERROR:NO, becuase this is 'internal':TIPS 3:only internal is not accessible.

C.fnC();//YES, becuase this is 'protected internal'

}


void fnD()

{

//

Console.WriteLine("fnD");

A.fnA();//YES, becuase this is 'protected'

//B.fnB();//ERROR:NO, becuase this is 'internal':TIPS 3:only internal is not accessible.

C.fnC();//YES, becuase this is 'protected internal'

}

}

}

}

but i am not getting any error at B.fnB()
i could access that method also
can u explain me
Posted by: lw48019908259 on: 12/28/2012 | Points: 25
Hi kishore,
With your explanation I am able to understand the differences between protected and internal
previously i think that protected can be accessible through out the assembly, but after your article i got clarification on these.
Thanks for your post.
Posted by: Munder2013 on: 5/13/2013 | Points: 25
Hi kishore,
Thanks for this precious post. it was difficult for me to understand the difference
but with the help of this post I cleared the concept.


Posted by: Nirai87 on: 6/17/2015 | Points: 25
Thanks for your post. I can easily understand the concept.
Posted by: Upendra09 on: 12/23/2015 | Points: 25
hi bro,
its really fabulous Artical.

but bro i m getting any error when i an internal is accessed in drived class in another asssembly.
i mean according this artical internal working as protected internal.

Have a look this please.

namespace Assembly_1 //This is first assembly.
{
//A normal public class which contains all different types of access-modifier classes in the assembly named 'Assembly_1'
public class Soul
{
//protected class A
protected class A
{
public static void fnA()
{
Console.WriteLine("fnA");
}
}

//internal class
internal class B
{
public static void fnB()
{
Console.WriteLine("fnB");
}
}

//protected internal
protected internal class C
{
public static void fnC()
{
Console.WriteLine("fnC");
}
}

//TIP 1:This class is inside the same class 'Soul' so everything is accessible from above.Hurray!!
class G
{
public static void fnG()
{
//All methods are easily accessible.Does not consider a modified indeed.
A.fnA();
B.fnB();
C.fnC();
Console.WriteLine("fnG");
}
}
}

//Different class but inside the same assembly named 'Assembly_1'
public class Soul_1
{
//Accesibility of 'protected members'::NO
//Accesibility of 'internal members'::YES
//Accesibility of 'protected internal' members::YES
//TIP 2:This class is NOT in same class 'Soul' but in the same assembly so only protected is NOT accessible rest all are accessible.
//IF you want to access protected then inherit class 'Soul'.
public void fnSoul_1()
{
//Soul.A.fnA();//ERROR:Accesibility of 'protected'::NO.ONLY WHEN Soul_1:Soul i.e. when Soul_1 inherits Soul.
Soul.B.fnB();
Soul.C.fnC();
Console.WriteLine("fnSoul_1");
}
}
}

namespace Assembly_2 //This is second assembly.
{
//TIP:if it does not inherit class 'Soul' then we can not access any thing beacuse this 'ANOTHER' assembly.
//TIP:only INTERNAL is NOT accessible , rest all are accessible from first assembly if it inherits class 'Soul'
class KISH : Assembly_1.Soul
{
//=======================================================================
class D
{
void fnD()
{
//
Console.WriteLine("fnD");
A.fnA();//YES, becuase this is 'protected'
B.fnB();//No getting any Error B.FnB() is too accessible here.
C.fnC();//YES, becuase this is 'protected internal'
}
}
}
}

Login to post response

Comment using Facebook(Author doesn't get notification)