|
Problem:
Your class has a private attribute or method. You want to specifically grant access to the attribute or method for a selected set of classes or subclasses, without opening access for anything else. Solution:
Use one of the following:
or
Discussion: There are two basic approaches for allowing access to an attribute or method on a class-by-class basis: declaring the allowable classes in the class the attribute or method is being declared in, or declaring those permissions in any specific subclasses. Note that the second approach breaks the encapsulation of the object, and so the first form is greatly preferred. Issue: Does allowing access for a given class "A" imply the same access for all subclasses of "A"? Issue: The second approach implies the use of a privacy-breaker such as that of the last recipe, above.
|