< Day Day Up > |
7.1 Access to Classes, Interfaces, Fields, and MethodsThe object-oriented Java language provides mechanisms for limiting access to classes, interfaces, fields, and methods. A class or an interface is said to be public if it is declared using the public keyword, in which case it may be accessed, using its qualified name , by any Java code that can access the package in which the class or interface is declared. A nonpublic class or interface may be accessed only from the package in which it is declared and is said to have default access . The effect of the public keyword on classes and interfaces is shown in Figure 7.2. Figure 7.2. Access Restrictions to Classes and Interfaces
A class member ”a field, a method, or a constructor ”may be declared using at most one of the public , private , or protected keywords.
The effect of the access modifiers on class members is shown in Figure 7.3. Note that every field or method of an interface must be public. Every member of a public interface is implicitly public, whether or not the keyword public appears in its declaration. If an interface is not public, every one of its fields and methods must be explicitly declared public. Figure 7.3. Effect of Access Modifiers on Members
|
< Day Day Up > |