Chris,

The C# language designers didn't have much choice, the decision was made by
the CLR designers (I suppose they could have been the same people though
;-)). Not sure why though, but it sort of makes sense to me (but see
http://jeroen.nu/ for why I'm not necessarily happy with it ;-)).

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.

Reply via email to