Considerations for Choosing Access Levels for Methods

Applying the optimal access levels to members of a class hierarchy makes the hierarchy easier to maintain by allowing you to control how such members will be used.

As a rule, you should declare class members with access modifiers that provide the least amount of access feasible. Restricting access to class members reduces naming conflicts and prevents methods from being used in unintended ways.

Internal class members should be declared as Private; such members are only accessible from within the class in which they are defined.

Methods that are only used within a class or by descendants of a class should use the Protected access modifier. A Protected member is accessible within the class in which it is declared, and from within any class derived from the class that declared this member.

Friend data members can be accessed from outside a class but only by modules that are part of the project where the class is defined.

Public data members are visible to everyone and are often used at the bottom of a class hierarchy.

See Also

Concepts

Class Hierarchy Design Considerations for Extensibility

Base Class Design Changes After Deployment