Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 26034 |  Welcome, Guest!   Register  Login
Home > Articles > OOPS > Understanding Protected, Internal and Protected Internal Access modifiers in OOPs

Understanding Protected, Internal and Protected Internal Access modifiers in OOPs

3 vote(s)
Rating: 5 out of 5
Article posted by Kishork80 on 7/2/2010 | Views: 14752 | Category: OOPS | Level: Beginner red flag

Advertisements

Advertisements
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

Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About kishor kumar

Experience:6 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, July 01, 2010
Level:Starter
Status: [Member]
Biography:Having 6 pyears of exp in dot net and still counting...
 Responses
Posted by: Sabarimahesh | Posted on: 16 Mar 2012 01:07:35 AM | Points: 25

Hi kishor kumar

I Got A Very Basic Knowledge about This Access Modifiers

Thnks YOu

Posted by: Sabarimahesh | Posted on: 16 Mar 2012 01:43:05 AM | 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 | Posted on: 16 Mar 2012 11:37:37 AM | Points: 25

This is assembly. Pleaz call the functions inside a main function

Posted by: Sudhakar162002 | Posted on: 17 Mar 2012 01:52:46 AM | 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 | Posted on: 19 Mar 2012 02:56:50 AM | Points: 25

Hi Kishor,
I'm Work out Ur Code it is fabulous thing thanks.....

Posted by: Hafeef | Posted on: 24 Jul 2012 10:31:25 AM | 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: 8019908259 | Posted on: 28 Dec 2012 02:00:18 AM | 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 | Posted on: 13 May 2013 08:23:51 AM | 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.



>> Write Response - Respond to this post and get points
Related Posts

In this article, we shall learn about different types of constructors in C#.NET.

Generally, it is the ability to appear in different forms. In oops concept, it is the ability to process objects differently depending on their data types. Its the ability to redefine methods for derived classes.

This article will explain you about inheritance by using a suitable example for the fresher's and for the working guy's to clear the OOP's concept.

Composition and Aggregation are the most confusing terms in OOPS. These 2 specifies relations between classes.

This article will discuss about the encapsulation in programming.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/20/2013 4:14:45 AM