Section 10.3. Protected Members


10.3. Protected Members

Chapter 9 discussed access modifiers Public and Private. A class's Public members are accessible wherever the program 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 not inherited by its derived classes. This section introduces the access modifier Protected. In Section 10.8, we discuss the access modifier Friend.

Protected access offers an intermediate level of access between Public and Private access. A base class's Protected members can be accessed only by members of that base class and by members of its derived classes.

All Public and Protected 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 can be accessed from the derived class by preceding the base class method name with keyword MyBase and a dot (.) separator. We discuss accessing overridden members of the base class in Section 10.4.

Software Engineering Observation 10.1

Derived class methods cannot directly access Private members of their base class. A derived class can change the state of Private base class instance variables only through non-Private methods provided in the base class and inherited by the derived class.


Software Engineering Observation 10.2

Declaring Private instance variables helps programmers test, debug and correctly modify systems. If a derived class could access its base class's Private instance variables, classes that inherit from that derived class could access the instance variables as well. This would propagate access to what should be Private instance variables, and the benefits of information hiding would be lost.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net