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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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.
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 :
From Java Language Spec
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.
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.
'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.