Packages in Java Programming Language

Hey everyone, in this tutorial, we will learn packages in Java Programming. Packages are the packaged form of java entities that include classes, interfaces, etc. We will deep dive into understanding packages and learn to implement them in Java.

Packages in Java Programming

Packages in Java Programming are a way to bring together a group of classes, interfaces and sub-packages. It is a group of related classes, interfaces, enumerations, etc.

Packages in Java Programming

Sub-packages in Java Programming: Packages that reside in another package are called sub-packages. They are not imported by default and must be imported manually if needed. No access specification is given to individual members of a sub-package and also they are considered as a different package.

Some existing packages in Java programming are:

  • java.lang – It comes bundled in Java by default.
  • java.io – Contains I/O related classes, methods, etc.

Why do we need packages in Java Programming?

  • To prevent naming conflicts.
  • To provide controlled access.
  • To achieve data encapsulation.

How to create a package in Java programming?

Let’s create a package named computer. Generally, the name of a package is spelt with lowercase letters. This is only to avoid name clashes with class names. We will create an interface named Pluggable that will reside within the computer package.

package computer;

interface Pluggable {
   public void pluggedIn();
   public void pluggedOut();
}

We will now create a class named PenDrive that will implement the above interface.

package computer;

public class PenDrive implements Pluggable {

   int storage = 64;

   public void pluggedIn () {
     System.out.println("Pen Drive is connected");
   }

   public void pluggedOut () {
     System.out.println("Pen Drive is removed");
   }

   public int storage() {
     return storage;
   }

   public static void main(String args[]) {
     PenDrive p = new PenDrive ();
     p.pluggedIn();
     System.out.println("Pen Drive Storage: " + p.storage());
     p.pluggedOut();
   }
}

How to create a hierarchy of packages in Java?

In Java, packages are named in reverse order to form the package hierarchy. Packages are very similar to directories or folders. We can consider any directory in a personal computer that holds one or more sub-folders; these actually form a hierarchy that is similar to the Java hierarchy of a package.

Consider a package named Asia.India.Kolkata, then these are the folders that exist i.e, Asia, India and Kolkata such that Kolkata is present in India and India is present in Asia. The main motive is to make classes easy to locate.

Types of packages in Java Programming

Built-in Packages in Java

These are the packages that consist of a large number of built-in classes that are a part of the Java API. Commonly used built-in packages are:

  • java.util packages in Java: This package contains a finite number of utility classes that are used to implement data structures such as Linked List, Sets, etc It also has support for Date and Time operations and much more.
  • java.net packages in Java: It contains classes that are used for network operations.

User-defined packages in Java

Packages that are defined by the user are known as User-defined packages. A user can manually create a package and have as many classes in them as he wants.

How to access a package from another package?

Accessing a package from another package in Java programming can be done in 3 simple ways:

  • Using asterisk in the import statement
    The asterisk (*) symbol is used to mean “all the things” in Java, using it, we can import everything that is inside the subpackage of a package.
    Example
    Consider a package named “tools” and we want to import everything that is inside this package, then we need to use the import statement as:
    import tools.*;
  • Using import package.ClassName;
    Mentioning a class name in the package is an efficient way to import only the necessary classes in the program, thus avoiding imports of unnecessary classes.
    Example
    Consider a package named “books” and we want to import only a specific class or interface from it (we will consider Pages class), then we can import just that one by using:
    import book.Pages;
  • Using a fully qualified name.
    There is a way to directly use a java package or its classes by using their fully qualified name. This way you need not import that package and can be used directly in the program.
    Example
    java.awt.List aSimpleList = new java.awt.List();

Size of a default package in Java Programming

By default, Java imports java.lang package. It has many classes that are generally used in a simple Java program such as String, Integer, etc. One of the most important classes is the Object class which in turn is also in java.lang package. The size of this package is based on its constituent i.e, 8 Interfaces, 37 classes, 3 enums, 27 exceptions, 23 types of errors and 5 types of annotations.

I hope now you have a better understanding of packages in Java Programming. To learn more content like this, stay tuned and follow us.