Reputation: 343
Why does the protected method not appear? If I make any public, internal or protected internal method they get appear.
class A
{
protected void AProtected() { }
}
class B:A
{
protected void BProtected() { }
}
class C:B
{
protected void CProtected() { }
}
class D:C
{
protected void DProtected() { }
}
class Program
{
static void Main(string[] args)
{
D classD = new D();
classD. //no method appear.
}
}
Upvotes: 0
Views: 1218
Reputation: 857
Another one to watch out for is if the method in the derived class is static. You can't call instance methods from a static method in a derived class, regardless of visibility/access modifiers.
If you have
namespace A
{
public class AClass
{
public void MethodCall()
{
xxx
}
}
}
Works
namespace B
{
public class BClass : A.AClass
{
public void Go()
{
A.AClass.MethodCall();
}
}
}
Doesn't Work
namespace B
{
public class BClass : A.AClass
{
public **static** void Go()
{
A.AClass.MethodCall();
}
}
}
Upvotes: 0
Reputation: 44439
Because protected
methods are accessible to the current class and subclasses of that class. Program
is not a subclass of D
.
A protected member is accessible within its class and by derived class instances.
Since I notice confusion: you can call those protected
methods from inside the subclass but you cannot call them from outside the subclass onto that subclass.
This might illustrate it:
MyMethod
is NOT accessible
MyMethod
IS accessible from the subclass
MyMethod
is NOT accessible
Upvotes: 4
Reputation: 186668
Protected methods can be called within derived classes only:
class A {
// This method can be called within derived classes only
protected void ProtectedA() {...}
// This method can be called within classes that are in the same namespace
internal void InternalA() {...}
// This method can be called either within derived classes only or
// within classes that are in the same namespace
protected internal void InternalOrProtectedA() {...}
}
class B: A {
protected void ProtectedB() {
// You can call A.ProtectedA() here since B is derived from A
}
}
class Program {
static void Main(string[] args)
// You can NOT call A.ProtectedA() here since B is NOT derived from A
// But you can call
// A.InternalA() since classes Program and A are in the same namespace
// A.InternalOrProtectedA() on the same reason
}
}
Upvotes: 0
Reputation: 39888
Because that's the whole idea of protected
. Protected members are only accessible by the declaring class and other classes that inherit from it.
You can find the official documentation here.
Upvotes: 1
Reputation: 7489
Protected is similar to private in that in can only be accessed from within the class, but has the added benefit of being accessible from inherited classes.
Your class Program
(and its main method) are not able to access private or protected methods of your other classes, only public.
Upvotes: 0
Reputation: 5636
Because protected
methods are not visible outside of the class or a class that inherits from that class.
From MSDN:
The protected keyword is a member access modifier. A protected member is accessible within its class and by derived class instances.
You could call DProtected()
within D
, but not outside of D
.
Upvotes: 0