Isn't "package private" member access synonymous with the default (no

I am a little confused over the term "package private" that some of the documentation uses, along with the usage of "default access." Aren't package private and default access both synonymous with protected?


Yes, it's almost the same. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition , by a subclass of its class in another package.


The "default" access modifier (the one where none of them are explicitly given) is "package-private", which means only things in the same package can access them. However, being in the same package implies nothing about the inheritance relationship between classes -- it's purely a naming convention.

"Protected" means that not only classes in the same package, but also subclasses (regardless of which package those subclasses are in) will be able to access it.


The default access for classes is package-private, however the default access for interface members is public.

eg

public interface I {
   int A = 1;
// same as
   public static final int A = 1;

   void method();
// same as
   public abstract void method();

   class C { }
// same as
   public static class C { }
}

The default access rules for interfaces are not the same as for classes.

链接地址: http://www.djcxy.com/p/4110.html

上一篇: Java类可访问性

下一篇: 是不是“包私人”成员访问与默认(同上)的代名词