Section 3.5.3 of the C# spec lays out some restrictions on accessing
protected members.  I don't think that Java has the analogous
restrictions and I'm trying to figure out why they are necessary
or desired.

Basically if an object has an instance typed as its base class
(or some class further up the inheritance chain) it cannot access
protected members of that instance.

Here's the example from the spec:

public class A
{
   protected int x;
   static void F(A a, B b) {
      a.x = 1;      // Ok
      b.x = 1;      // Ok
   }
}
public class B: A
{
   static void F(A a, B b) {
      a.x = 1;      // Error, must access through instance of B
      b.x = 1;      // Ok
   }
}

Suppose that the A we were passed in B.F was an instance of C - another
subclass of A, like this:

public class C: A
{
   protected new int x;
}

In this case we would still know which x to update (the one in A), right?

Can anybody explain the justification for this restriction?


Chris Daly
Rational Software

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to