Packages in JAVA
Packages in JAVA
Packages in Java are a mechanism that encapsulates a group of classes, sub-packages, and
interfaces. Packages are used for:
Prevent naming conflicts by allowing classes with the same name to exist in different
packages, like college.staff.cse.Employee and college.staff.ee.Employee.
They make it easier to organize, locate, and use classes, interfaces, and other components.
Packages also provide controlled access for Protected members that are accessible within
the same package and by subclasses.
Also, for default members (no access specifier) that are accessible only within the same
package.
Directory Structure: Package names and directory structures are closely related. For example, if a
package name is college.staff.cse, then three directories are, college, staff, and cse, where cse is
inside staff, and staff is inside the college.
Naming Conventions: Package names are written in reverse order of domain names,
e.g., org.geeksforgeeks.practice. In a college, the convention might be:
college.tech.cse
college.tech.ee
college.art.history
Example:
import java.util.*;
In Java, we can import classes from a package using either of the following methods:
import java.util.Vector;
This imports only the Vector class from the java.util package.
import java.util.*;
Types of Java Packages
Built-in Packages
User-defined Packages
1. Built-in Packages
These packages consist of many classes which are a part of Java API.Some of the commonly
used built-in packages are:
java.lang: Contains language support classes(e.g classes which defines primitive data
types, math operations). This package is automatically imported.
java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
java.awt: Contain classes for implementing the components for graphical user interfaces
(like button,; menus etc). 6)
2. User-defined Packages
o.getNames(s);
}
}