Protected access for instance members
When a protected instance member is accessed outside the program text of the class in which it is declared, and when a protected internal instance member is accessed outside the program text of the program in which it is declared, the access must take place within a class declaration that derives from the class in which it is declared. Furthermore, the access is required to take place through an instance of that derived class type or a class type constructed from it. This restriction prevents one derived class from accessing protected members of other derived classes, even when the members are inherited from the same base class. Let B be a base class that declares a protected instance member M, and let D be a class that derives from B. Within the class-body of D, access to M can take one of the following forms: · An unqualified type-name or primary-expression of the form M. · A primary-expression of the form E.M, provided the type of E is T or a class derived from T, where T is the class type D, or a class type constructed from D · A primary-expression of the form base.M. In addition to these forms of access, a derived class can access a protected instance constructor of a base class in a constructor-initializer (§10.11.1). In the example public class A static void F(A a, B b) { public class B: A within A, it is possible to access x through instances of both A and B, since in either case the access takes place through an instance of A or a class derived from A. However, within B, it is not possible to access x through an instance of A, since A does not derive from B. In the example class C<T> class D<T>: C<T> the three assignments to x are permitted because they all take place through instances of class types constructed from the generic type.
|