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:
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!!
Anand Upadhyay
No comments:
Post a Comment