Feb 4, 2009

protected access

public class A
{
protected virtual void Foo()
{
Console.WriteLine("A.Foo");
}
}
public class B : A
{
public void CallMethod(A a)
{
a.Foo();
}
}
.........
static void Main()
{
B b = new B();
b.CallMethod(new A());
}
B can access the protected member of A which it inherits from i.e. A::Foo() or simply Foo() because that is a member of B now and it has access to that but not a.Foo() because for B "a" is just another instance of a class and one cannot access the protected members of a variable you create.

No comments:

Post a Comment