i created an application depicting the protected access modifiers, using the sample provided on MSDN
site, but it seems to be error prone, below is the code i am using:
and below is the sample from MSDN site:
i created an application depicting the protected access modifiers, using the sample provided on MSDN
site, but it seems to be error prone, below is the code i am using:
and below is the sample from MSDN site:
Your AccessModifiersCSharp
class which is where Main
is defined does not inherit from Class1
, so has no access to its protected
members.
You can access Method1
from your Class2
as it does inherit from Class1
.
The difference between your code and the MSDN code is that class B
that contains Main
inherits directly from A
.
protected members of class A can only be accessed from classes derived from A.
This means Class2 can access Method1(), but not AccessModifiersCSharp
see msdn
If you want your code to compile, you need to declare Method1() as public or internal.
Put the Main() method inside Class2 instead of the class AccessModifiersCSharp! The AccessModifiersCSharp does not derive from Class1 and thus has no access to its protected members and methods.
In the article that you referenced, Main is a method of B, which is why it has access to protected members of B, and, through inheritance, A's protected members.
A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member
it's mean: you can access Methode1 in Class1 methods And Class2 Methods. and You can access Method2 only in Class2 methods.
Main() Method in AccessModifiersCSharp class. and only can access to public method of Class1 and Class2.