Introduction
We have repeatedly stated that one of the main features of OOP is its ability to reuse the code already created. One way of achieving this is by extending the classes and implementing the interfaces. This is limited to reusing the classes within a program. We can use classes from other programs without physically copying them into the program under
development by using a concept is known as packages. Another way of achieving the reusability in Java is to use packages.
Packages are Java's way of grouping a variety of classes and/or interfaces together. The grouping is usually done according to functionality. In fact, packages act as a containers for classes.
By organizing our classes into packages we achieve the following benefits:
1. The classes contained in the packages of other programs can be easily reused.
2. In packages, classes can be unique compared with classes in other packages. That is, two classes in two different packages can have the same name. They may be referred by their fully qualified name, comprising the package name and the class name.
3. Packages provide a way to hide classes thus preventing other programs or packages from accessing classes that are meant for internal use only.
4. Packages also provide a way for separating design from coding. First we can design classes and decide their relationships, and then we can implement the Java code needed for the methods. It is possible to change the implementation of any method without affecting the rest of the design.
For most applications, we will need to use to different sets of classes, one for the internal representation of our program's data, and the other for external presentation purposes. We may have to build our own classes for handling our data and use existing class libraries for designing user interfaces.
Java packages are therefore classified into two types. The first category is known as Java API packages and the second is known as user defined packages.
Java API Packages
Java API provides a large number of classes grouped into different packages according to functionality. Most of the time we use the packages available with the Java APL.
Package name Contents
java.lang Language support classes. These are classes that Java compiler itself uses and therefore they are automatically imported. They include classes for primitive types, strings, math functions, threads and exceptions.
java.util Language utility classes such as vectors, hash tables, random numbers, date, etc.
java.io Input and output support classes, They provide facilities for the input and output of data.
java. awt Set of classes for implementing graphical user interface. They include classes for windows, buttons, lists, menus and so on.
java.net Classes for networking. They include classes for communicating with local computers as well as with internet servers.
java.applet Classes for creating and implementing applets.