Java Packages

Java Package is a group of related types (classes, interfaces, enumerations, annotations and sub packages). Programmer can either create his/her own package or use the pre defined packages provided in JDE.

Subpackage

A package can have multiple packages inside it. A subpackage is the package inside a package. To further categorize a package, we use subpackages. For example, java is a package that has subpackages like java.util, java.sql etc.

Types of Java Package

A java package can be divided into two categories:

  • Built-in Java Package
  • User defined Java Package

Built-in Java Package

Java Development Environment JDE comes a built-in Java Package that has all the pre-defined classes in it. These classes are available for free and programmer can use them in the code. The below image shows the hierarchy of main java package. It shows some of the sub packages and classes it contains.

Java Package
Java Package

There are many built-in packages in JDE such as java, lang, util, awt, javax, swing, net, io, sql etc. These packages are used to perform specific fucntions:

  • java.lang: groups fundamental classes
  • java.io: groups input output classes

User defined Java Package

User can create his/her own java packages. The package keyword is used to create a package. To create a package, the package name is written after the package keyword. It should be the first line in the source code file. The best practice is to write the first letter of package in lower case to avoid conflict with interface and class names. A file must contain only one package.

Here is an example that shows how to create a package:

/* File name : Book.java */
package books;

interface Book {
   public void name();
   public void price();
}

The below piece of code shows how to implement the interface in the same package:

package books;
/* File name : FictionBook.java */

public class FictionBook implements Book {

   public void name() {
      System.out.println("Harry Potter");
   }

   public void price() {
      System.out.println("12.99 $");
   } 

   public int noOfPages() {
      return 400;
   }

   public static void main(String args[]) {
      FictionBook obj = new FictionBook();
      obj.name();
      obj.price();
   }
} 

The next step is to compile both these file on command line. The commands to compile the files will be:

$ javac -d . Book.java 
$ javac -d . FictionBook.java

The output of the above codes after compilation is:

A package should only contain one public class in one file. If programmer needs to add more than one public classes in a package then a new file must be created for each new public class. The name of file should be same as the name of public class. In this way a package can have multiple public classes.

Import Package

The keyword import is used to import any java package. import statements are written in the beginning of java file. The (*) operator is used to access all classes of a package. The below statement imports all classes of java.util package. The syntax to import all classes of a package is:

To access only a specific class of a package, programmer can use the following import statement. The class name is written with dot operator (.) to use that particular class. The below statement imports only the class Map of java.util package.

There is another way of importing a specific class of a package without using the import keyword. This method involves using the fully qualified name of class. Programmer can create an object of the class using the new keyword. The syntax is:

The fully qualified name method is usually used when two classes have methods having same name.

Advantages of Java Package

Java Package has fundamental importance due to its multiple advantages, a few of them are listed below, a java package:

  • Provides simplicity by grouping together similar classes and interfaces.
  • Is easy to maintain and provide better control access.
  • Makes searching and locating the functionality easier.
  • Prevents naming conflicts as each package creates a new namespace.