|
The public modifier is the least restrictive of all access modifiers. You can apply it to a class, its methods and its member variables. A public class can be instantiated without any restrictions. The public methods or member variables can be accessed unrestrictive if you could access the class in which they are declared. Hence, you can use them to access the members of the class. Listing 3.8 has two class definitions. The public Cat class declares a public member variable, color and a public metod meow().

|
|
Accessing and instantiating a public class Cat |
|
|
Accessing a public member variable |
|
|
Invoking a public method |
The public members of public class Cat are accessible to all the classes running in that Java environment. For example, the class Crocodile resides in different package than the Cat class, but it can still instantiate the Cat class because Cat is a public class. The Crocodile class can also access the public members of the Cat class. Note that if a class is declared as public, it can be instantiated by any class without any restriction, but access to its members depends on their own access modifier. For example, the public class Cat in listing 3.6 can be instantiated by any class. However, its default member secretAboutCat is accessible only to the classes within the package, com.example.pets. For instance, the class Crocodile cannot access secretAboutCat.
|
|
For the exam, you need to know the combined effect of access to the class and access to its members. (For instance, accessing private methods of public class). To decide whether a particular member is accessible or not, first look at the access level of class. If the class is not accessible. (For example, the class has default access and you are trying to access it from different package) then its members inaccessible irrespective of their own access-level. Once you are sure that the class is accessible, consider the access levels of individual members to see if they are accessible. |
Understanding accessibility with sample class hierarchy
Public members of a public class are accessible to all the classes running in that Java environment. Figure 3.4 explains the public access of the Cat class with the Animal class hierarchy.
![]() |
Figure 3.4 All classes have access to the public members of public class Cat
Our convention is to show the classes with access in a dark-gray color. Since all classes have access to the public members of Cat class, they all are shown with the dark gray color. Note that even the classes which are not part of Animal hierarchy, can access the Cat class and its public members. If the class Cat were not public, it would have to have the default access, and the classes that are not in the com.example.pets package would not have access to it. In that case, they would not have access to its public members either.
|
|
Note that the Java interface by default always has only public methods and variables. In part-I we saw how the Java interface describes certain common behavior in the form of set of methods, which any class can implement. But for implementing this common behavior, the interface methods must be accessible to everyone. Therefore, it is reasonable that the Java interface by default always has only public methods and variables. |