|
When a class, method or variable declaration does not have any access modifier, it will have a “default or package” access level. A class or its members with default access are available to all the classes within the same package in which the class is declared. For example, if you declare a class with default access, it can only be instantiated by the classes within the same package. Similarly, the member variables or methods with default access can only be accessed by the classes within the same package. Listing 3.2 shows three class definitions. The Cat class defines a member variable secretAboutCat with default access, as it is declared without any access modifier. Both classes HouseCat and Chameleon can successfully access this variable.

Note that the variable secretAboutCat of a Cat instance is accessible from the Chameleon class as it is defined in same package (com.example.pets) as the Cat class. It is also true for the HouseCat class. The HouseCat class can access default members of Cat class as it is in the same package as Cat and not because it is a subclass of Cat.
Inheritance and default modifier
In Listing 3.2, the HouseCat class inherits the default members of the Cat class because it is its a subclass. Note, however that it cannot access the inherited default members if it would have been defined in another package. For example, the BobCat class (see figure 3.2) in the example hierarchy is also a subclass of Cat class. But it won’t be able to access default members of the Cat class because it does not belong to the same package as Cat class.
Listing 3.3 illustrates this by accessing the inherited default members of the Cat class.

|
|
To correctly address the SCJP questions related to accessing a method or variable, always consider these three things- 1.What the access modifier is. 2. The package declarations of both classes. 3. Whether the method or the variable is inherited. |
Understanding accessibility with sample class hierarchy
Figure 3.2 explains the default access with the same sample class hierarchy for Animal class that we used earlier. The classes, which can access the default members (such as secretAboutCat) of Cat are shown with the dark gray color. As you can see all the classes in package com.example.pets have the access to default members of Cat.

Figure 3.2 Classes in the same package as Cat class have access to the (default) members of Cat class
Note that all classes including those which are not part of Animal hierarchy (and which are not shown in figure) in com.example.pets will have access to the default members of class Cat. For example, if you have a class named RoboDog in com.example.pets package, it can also access default members of Cat.
Other classes, which are outside the com.example.pets package, can instantiate the Cat class, as it is declared as public class. However, they cannot access its default members. If you declare the Cat class itself as default (that is without any access modifier), then it could only be instantiated by the classes in the package com.example.pets. In that case, only the classes indicated by the dark-gray color in the figure, namely the Chameleon and HouseCat could instantiate it.