Whoops. I wasn't quite awake yet. In Java it isn't legal either. Well, the example below would be legal, but that is because protected members also have package (or default) access. If you move A and B to two different packages, the below code doesn't compile in Java either.
Regards, Jeroen > -----Original Message----- > From: Moderated discussion of advanced .NET topics. > [mailto:[EMAIL PROTECTED]] On Behalf Of Chris Daly > Sent: Friday, December 13, 2002 01:48 > To: [EMAIL PROTECTED] > Subject: [ADVANCED-DOTNET] Reasons for restrictions on > protected access from subclasses? > > > 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. > You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.