What is package access in Java?

It is more convenient to use related classes if you group them together within a unit known as a package. In fact, the Java Class Library is organized into packages.

A public class—whether it is within a package or not—is available to any other class. If you omit the class’s access modifier entirely, the class has default access and is only available to other classes within its package. This kind of class is said to have package access.

A default access modified class can only be accessed within its package. This class cannot be accessed outside the package.

Package access is more restricted than protected access and gives you more control when defining classes. You can use package access in situations where you have a package of cooperating classes that act as a single encapsulated unit. When you control the package directory, you control who is allowed to access the package. This is also referred to as default package access modifier.

Here is an illustration of the various kinds of access:

Public, private, protected, and package access of the data fields and methods of a class C
Public, private, protected, and package access of the data fields and methods of a class C
Modifier Class Package Sub-class Global
Public Yes Yes Yes Yes
Private Yes No No No
Package Yes Yes No No
Protected Yes Yes Yes No

Free Resources