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?

- 303,325
- 100
- 852
- 1,154

- 2,047
- 2
- 17
- 17
-
7So, there is no keyword whatsoever to express package private access? It is only implied by not specifying the access modifier? – TurtleToes Mar 24 '11 at 07:58
-
5TurtleToes, that's right, and this can be a bit confusing in Java 8, since it's actually possible to put `default` modifier in front of (interface) methods. But that's not an access modifier! All access modifiers and their scopes is visualized clearly in [this table](http://stackoverflow.com/a/33627846/276052). – aioobe Nov 13 '15 at 13:16
-
1you might also check the tutorial table from http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – childno͡.de Feb 01 '16 at 21:28
7 Answers
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.

- 10,037
- 10
- 48
- 56
-
There were a lot of good answers here, but this one explained it simple and to the point, so I'm accepting it – TurtleToes Mar 24 '11 at 07:52
-
25Its worth noting that the default access for interface member is *not* package-private. – Peter Lawrey Mar 24 '11 at 09:01
-
1@PeterLawrey Oh? What is the default access for interface members, then? – ArtOfWarfare Oct 31 '12 at 18:59
-
10The default for a field is `public static final`, for a method is `public abstract`, for an enum or annotation is `public` and for a class it `public static` – Peter Lawrey Oct 31 '12 at 20:43
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.

- 16,629
- 6
- 56
- 82
-
well your wording about protected is wrong.. Same package class instances also can access protected members.. – Gursel Koca Mar 24 '11 at 07:45
The default access for classes is package-private, however the default access for interface members is public.
e.g.
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.

- 525,659
- 79
- 751
- 1,130
Package-private and default access are synonyms. An object can also access protected member of the objects whose classes are in the same package. An object can also access protected member of its superclasses without a condition about their package. As a concrete example :
package ab;
class A {
protected void foo() {}
void dd(){}
}
class C {
void aa(){
A a = new A();
a.foo(); //legal
a.dd(); //legal
}
}
package sub;
class D extends A{
void ac(){
foo(); //legal ..
dd(); //illegal.. because dd has default access..
}
class E {
void ee(){
A a = new A();
a.foo(); //illegal
a.dd(); //illegal
}

- 5,782
- 5
- 28
- 71

- 20,940
- 2
- 24
- 34
-
downvoter could explain what is wrong of this explanation?.. except bad wording.. – Gursel Koca Mar 24 '11 at 07:39
-
6I'm not the downvoter, but I guess it's because it's not quite right; an object can access *protected* members of superclasses, regardless of package. – Adrian Petrescu Mar 24 '11 at 07:42
'Package private' and default access are the same. In early releases of the compiler around 1.1.2/3, 'package' was an allowed modifier, but ignored, meaning the same as no modifier, i.e. 'package private'. Shortly afterwards there was a short lived fashion for putting /*package*/
(as a comment) in such situations. Similarly at that time you could declare things like synchronized classes, although again there was no actual semantic effect.
Neither of them is the same as 'protected', which extends to derived classes in other packages.

- 305,947
- 44
- 307
- 483
From Java Language Spec
- 6.6.5 Example: Default-Access Fields, Methods, and Constructors If none of the access modifiers public, protected, or private are specified, a class member or constructor is accessible throughout the package that contains the declaration of the class in which the class member is declared, but the class member or constructor is not accessible in any other package.
If a public class has a method or constructor with default access, then this method or constructor is not accessible to or inherited by a subclass declared outside this package

- 12,427
- 23
- 80
- 112
-
2But what about "package private". That isn't in the JLS. – Tom Hawtin - tackline Mar 24 '11 at 09:18
default and package-private both are same, which means both can be used by any class till they are in same package.
The package-private term, actually, is termed by the meaning of private modifier as private means it is available only in same class and no other classes or subclasses can access it within same package or without.
Hence package-private means same as default.

- 3,929
- 13
- 37
- 40

- 53
- 6