In this article we learn the what is packages in java, advantages of using packages, types of packages and how packages work in java.
What is Packages in Java?
Packages in java is a collection of similar types of Java entities such as classes, interfaces, subclasses, exceptions, errors, and enums. A package can also contain sub-packages. Packages are essential for better management and accessing of code.
Advantages of Using Packages in Java
1. Make easy searching or locating of classes and interfaces.
2. Avoid naming conflicts. For example, there can be two classes with the name Student in two packages, university.csdept.Student and college.itdept.Student.
3. Implement data encapsulation (or data-hiding).
4. Provide controlled access: The access specifiers protected and default have access control on package level. A member declared as protected is accessible by classes within the same package and its subclasses. A member without any access specifier that is default specifier is accessible only by classes in the same package.
5. Reuse the classes contained in the packages of other programs.
6. Uniquely compare the classes in other packages.
7. Package names are dot-separated, e.g., java.lang.String
8. Packages avoid namespace collision: A package cannot contain two classes with the same names, but two different packages can have a class with the same name.
9. The exact name of the class is identified by the structure of its package.
Types of Packages in Java
They can be divided into two categories:
1. Java API packages or built-in packages
2. User-defined packages
1. Java API packages or built-in packages
Java provides a large number of classes grouped into different packages based on a particular functionality.
Examples:
java.lang: It contains classes for primitive types, strings, math functions, threads, and exceptions.
java.util: It contains classes such as vectors, hash tables, dates, Calendars, etc.
java.io: It has stream classes for Input/Output.
java.awt: Classes for implementing Graphical User Interface – windows, buttons, menus, etc.
java.net: Classes for networking
java.Applet: Classes for creating and implementing applets
2. User-defined packages
As the name suggests, these packages are defined by the user. We create a directory whose name should be the same as the name of the package. Then we create a class inside the directory
How Packages in Java Work?
1. The names of packages and the directory structure are closely related to each other.
2. For example, if a package name is university. engineering. itdept, then there are three directories- university, engineering, and itdept such that itdept is present in engineering and engineering is present in university.
3. The package university can be considered as a top-level package while engineering is a sub-package of university and itdept is a sub-package of engineering.
Accessing Packages or Classes from Another Package
If we want to access all the classes and interfaces of an existing package, then we use the import statement. We can do it in three different ways:
- import package. *;
- import package. classname.
- fully qualified name.
By using * after the import statement, we can access all the classes of the package but not the sub-packages.
Important points on Packages in Java
1. Every class belongs to a package. If you do not mention any package, the classes in the file move into a special unnamed package which is the same for all the files which do not belong to a specified package.
2. Multiple classes and interfaces in a file can be part of the same package.
3. If the package name is specified, then the directory name must match the package name.
4. We can access the classes declared as public in another package using: import package-name. Class-name