Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't "package private" member access synonymous with the default (no-modifier) access?

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?

like image 310
TurtleToes Avatar asked Mar 24 '11 07:03

TurtleToes


People also ask

Is package-Private same as default?

If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

What is difference between package and private access modifiers?

Private modifier is the most restricted modifier among all modifiers. Protected modifier is more accessible than the package and private modifier but less accessible than public modifier. Package modifier is more restricted than the public and protected modifier but less restricted than the private modifier.

What is the default access modifier when no modifier is specified?

Class, record, and struct accessibilityinternal is the default if no access modifier is specified. Struct members, including nested classes and structs, can be declared public , internal , or private .

Is package an access modifier?

package-private is the default access modifier and does not have a keyword, because package is used to specify the package for a class or interface. To declare package access for something, use no access modifier.


2 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.

like image 193
Luciano Fiandesio Avatar answered Oct 03 '22 22:10

Luciano Fiandesio


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.

like image 40
Adrian Petrescu Avatar answered Oct 03 '22 22:10

Adrian Petrescu