Information Technology Reference
In-Depth Information
This code produces the following output:
Total: 6
Figure 23-8. Objects of a nested class and its enclosing class
Visibility and Nested Types
In Chapter 7, you learned that classes, and types in general, can have an access level of either
public or internal . Nested types, however, are different, in that they have member accessibility
rather than type accessibility . Therefore, the following are true:
￿
A nested type declared inside a class can have any of the five class member accessibility
levels: public , protected, private , internal , or protected internal .
￿
A nested type declared inside a struct can have one of the three struct member accessi-
bility levels: public , internal , or private .
In both cases, the default access level of a nested type is private , which means it cannot be
seen outside the enclosing type.
The relationship between the members of the enclosing class and the nested class is a little
less straightforward and is illustrated in Figure 23-9. The nested type has complete access to
the members of the enclosing type, regardless of their declared accessibility, including mem-
bers that are private and protected .
The relationship, however, is not symmetrical. Although the members of the enclosing
type can always see the nested type declaration and create variables and instances of it, they do
not have complete access to its members. Instead, their access is limited to the declared access
of the nested class members—just as if the nested type were a separate type. That is, they can
access the public and internal members, but cannot access the private or protected mem-
bers of the nested type.
Search WWH ::




Custom Search