Answer» In Java, a package is a container for a collection of classes, sub-packages, and interfaces. Packages are used for the following purposes:
- Keeping name problems at bay. For example, in two packages, college.staff.cse.Employee and college.staff.ee.Employee, there could be two classes named Employee.
- Making it easier to search for, locate, and use classes, interfaces, enumerations, and annotations
- Controlling access: package level access control is available in both protected and default modes. Classes in the same package and its subclasses can access a protected member. Only classes in the same package have access to a default member (which has no access specifier).
- Packages help in data encapsulation (or data-hiding).
Subpackages - Subpackages are packages that are contained within another package. These are not automatically imported; they must be imported manually. Members of a subpackage also have no access privileges, therefore they are treated differently by protected and default access specifiers. Types of packages:-
The above image shows that packages can be classified into two broad categories - User Defined Packages and In-Built Packages. Built-in Packages: These packages contain a huge number of classes that are included in the Java API. The following are some of the most often used built-in packages as shown in the image below:
java.lang: This package contains language-specific classes (e.g classes that define primitive data types, maths operations). This package will be imported automatically.
java.io: This package contains classes that handle input and output operations.
java.util: This package contains utility classes that provide data structures such as Linked Lists, Dictionary, and Date/Time operations.
java.applet: This package contains Applet-related classes.
java.awt: Contains classes for implementing graphical user interface components (like buttons, menus etc).
java.net: This package contains classes that help with networking.
User-defined packages: These are the packages that the user has defined.
|