protected Members
Chapter 9 discussed access modifiers public, private, and internal. A class's public members are accessible wherever the application has a reference to an object of that class or one of its derived classes. A class's private members are accessible only within the class itself. A base class's private members are inherited by its derived classes, but are not directly accessible by derived class methods and properties. In this section, we introduce access modifier protected. Using protected access offers an intermediate level of access between public and private. A base class's protected members can be accessed by members of that base class and by members of its derived classes. (Members of a class can also be declared protected internal. A base class's protected internal members can be accessed by members of that base class, by members of its derived classes and by any class in the same assembly.)
All non-private base class members retain their original access modifier when they become members of the derived class (i.e., public members of the base class become public members of the derived class, and protected members of the base class become protected members of the derived class).
Derived class methods can refer to public and protected members inherited from the base class simply by using the member names. When a derived class method overrides a base class method, the base class version of the method can be accessed from the derived class by preceding the base class method name with the keyword base and the dot (.) operator. We discuss accessing overridden members of the base class in Section 10.4.
|
Relationship between Base Classes and Derived Classes
|