Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Member Access Control

 

Every member of a class, be it a field or a method, has an access control attribute which affects the manner in which that member can be accessed. The members of a class can be private, public, protected, internal, or protected internal. For example, the fields real and imag declared in Program gif are both private. Private members can be used only by methods of the class in which the member is declared.

On the other hand, public members of a class can be used by any method in any class. All of the operations defined in Programs gif, gif and gif all declared to be public.

In effect, the public part of a class defines the interface to that class and the private part of the class encapsulates the implementation of that class. By making the implementation of a class private, we ensure that the code which uses the class depends only on the interface and not on the implementation of the class. Furthermore, we can modify the implementation of the class without affecting the code of the user of that class.

Protected members are similar to private members. That is, they can be used by methods of the class in which the member is declared. In addition, protected members can also be used by methods of all the classes derived from the class in which the member is declared. The protected category is discussed again in Section gif.

An internal member can be accessed by any method in the same program in which the member is declared. Finally, a protected internal member can be accessed by methods in the class in which the member is declared, by methods in classes dervied from the class in which the member is declared, and any method in the same program in which the member is declared.


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.