Java Reference
In-Depth Information
PITFALL: Forgetting about the Default Package
When considering package access, do not forget the default package. Recall that all the
classes in your current directory (that do not belong to some other package) belong to
an unnamed package called the default package . So, if a class in your current directory
is not in any other package, then it is in the default package. If an instance variable or
method has package access, then that instance variable or method can be accessed by
name in the definition of any other class in the default package.
PITFALL: A Restriction on Protected Access
The situation described in this pitfall does not occur often, but when it does, it can be
very puzzling if you do not understand what is going on.
Suppose class D is derived from class B , the instance variable n has protected access
in class B , and the classes D and B are in different packages, so the class defi nitions begin
as follows:
package one;
public class B
{
protected int n;
...
}
package two;
import one.B;
public class D extends B
{
...
}
Then the following is a legitimate method that can appear in the definition of class D :
public void demo()
{
n = 42; //n is inherited from B.
}
 
Search WWH ::




Custom Search