One of the main features of OOP is its ability to reuse the code already created. One way of achieving this is by using the classes from other programs, without physically copying them into the program you are currently +creating. This concept in Java is called as packages, while in other languages is called as class libraries. Packages are a way of grouping a variety of classes and/or interfaces together. The grouping is done according to the functionality. Therefore we can say packages act as containers for classes. By organizing our classes into packages we can achieve following benefits:
The classes of other programs contained in the packages can be easily used.
In packages, classes can be unique compared with classes in other packages i.e. two classes in two different packages can have the same name. They may be referred by their full name comprising of the package name and the class name.
Packages provide a way to hide classes.
Packages also provide a way for separating design from coding.
In Java packages are classified into two types:
1. Java System packages (examples below)
2. User-defined packages (next section)
Java System packages: Java system provides a large number of classes grouped into different packages according to the functionality. In a Java program, we use the packages available with the Java system. Here are few commonly used packages: