A package is defined as a collection of built-in classes. Packages in Java can be classified into two types namely:
Java API provides a collection of packages to be used in Java applications called built-in packages. While developing Java applications, a programmer can create his own packages. These packages are called User Defined packages.
To create our own package, we have to implement the following steps:
bin
whose name is the same as the package’s name. For example, create a directory with the name MyPack
.Java class
whose first statement of the file should
be a package statement, package MyPack;
Java file
.bin
directory. Now, the classes in the new package can be imported to our Java applications using the import
statements,
import MyPack.*;
Access Specifiers specify the way in which a class, an interface, or abstract class and their members are accessed. Java supports four types of access specifiers:
public
: Any java element that is declared as public is accessible to the members of the same class, the classes in the same directory, or the classes in another path.private
: Any java element that is declared as private can be accessible to the members of the same class, only. It cannot be available for the classes that are on the same path or outside the path.protected
: Any java element that is declared as protected is accessible to the same class members or to its sub-classes within the same path or outside the path.Default Access specifier
:
This means that a Java element is not declared as public, private, or protected. This default specifier will act as public for the same class members or other classes that are on the same path. It will act as private for the classes that are defined outside the path. And will act as protected for the derived classes of the same path and outside the path.