Skip to main content

Java Packages

Packages In Java:


Package is a container for the classes that are used to keep the class name space compartmentalized.
In other words,  Java Packages are nothing more thanthe way we organize files into different directories according to their functionality, usability as well as category they should belong to. A Java package is a Java programming language mechanism for orgranizing classes into name spaces.
Example:
You can contain all classses related to all sorting programs in your own package.


We can keep different classes of same type in a same package for better glance of the project.
It allows fexibility to give same name but to many classes, that is to avoid name space collision. The packages in Java provides mechanism for partitioning the class name space into more manageable chunks.
Infact, package is both a naming and a visibillity control mechanism.
It supports reusability and maintainability of the Java codes.

Advantages of Packages:

  • Packages provide code reusability, because a package contains gorup of classes.
  • It helps in resolving naming collision when multiple packages have classes with the same name.
  • Package also provides the hiding of class facility. Thus, other programs cannot use classes from hidden package.
  • Accessing limitation can be applied with the help of packages.
  • Nesting of a package, that is, one package can be defined in another package in a heirarchy fashion.

Types of Packages:

  1. Built-in Packages
  2. User-defined Packages

Built-in Packages in Java :

 In Java, already many predfined packages are available, those are to help programmers to develop their software in easy way. In Java, we have several built-in packages called API(application program interface), for example when we need user input, we import a package like this:
  import java.util.Scanner;
Here:
  • java is a top level package
  • util is a sub package in java
  • Scanner is a class which is present in the sub package util.
Java supports many built-in packages like :

  1. Core Packages :
    1. java.lang
    2. java.io
    3. java.util
    4. java.net
  2. Window Toolkit & Applet:
    1. java.awt
    2. java.awt.image
    3. java.applet
    4. javax.swing
javax.swing is the latest addition in Java version 5  and above.

There are many other packages like java.sql for the Java Database connectivity which are present in the Java Development Kit.

Using API Packages:

A package is a collection of classes and each class is a collection of members and methods. Any class as well as any member and method in  a package are accessible from a Java program. This can be achieved in Java by import keyword or statement.
There are two ways of using import statement :-
  1.  With fully qualified class name
    • When it is required to access a particular class For example: java.lang.String :- java.lang is a package in java which contains a class called String. It will only import the class String present in the java.lang package.
  2. With default (.*) quantification 
    • When it is required to access a number od classes
    • For Example : java.lang.* :- java.lang is a package in Java which conatains different classes. Now, if we write in this way then every classes and methods present in the java.lang package will be imported. This is not specific now you can access any class or method present in this package.

User-Defined Packages:

Programmers like you can also make your own packages. Such packages are called user-defined packages. When you defined your own package you can use it in any other main packages by using statement called import.

Using User-Defined Packages:

One can easily define his/her own package in following way:


package <name_of_the_package>;
public class <name_of_the_class>{
      Statements.....;
}

Example:

Now in order to import the package defined above we use a keyword called import in following way:

 import  <package_name>.<class_name>;
public class <another_class_name>{
     Statements.....
}

Example:

Different ways of importing API packages :

  1. Using import statement:
    • packages can be imported into the codes using import statement in Java programming. It can be done in following way:-
  2. Without using import statement:
    • However, instead of importing whole package or class it is possible to refer a class in order to instantiate an object. It can be achieved in following way:-


Package naming conventions:-

  •  Packages are usually defined using a hierarchial naming pattern, with levels in the hierarchy separated by periods (.) .
  •  Although packages lower in the naming hierarchy are often referred to as 'sub-packages' of the corresponding packages higher in the hierarchy, there is no semantic relationship between packages.

Organizational Package naming conventions:-

(This is not a rule in Java but is a good practice.)
  • Package names should be all lowercase characters whenever possible.
  • Frequently, a package names begins with the top level domain name of the organization and then the organization's domain and then any subdomains listed in the reverse order.
  • The organization can then choose a specific name for their package.
  • In addtion to this, following steps must be taken into consideration:-
    • Use package statement at the beginning of the package file.
    • Define the class that is to be put in the package and declare it as public.
    • Create a sub-directory under the working directory with the same name as the package name.
    • Store the file with the same name as the class Name.java in the sub-directory created.
    • Store the compiled version (i.e., .class) file into the same sub-directory.

Design guideline package cohesion:-

  • Only closely related classes should belong to the same package for the efficiency, maintainability and readability.
  • Classes that change together should belong to the same package.
  • Classes that are not reused together should not belong to the same package.

More on user defined packages:-

    Note:-

    • We cannot put two or more public classes together in a .java file, otherwise there will be an ambiguity in naming the .java file. Then, How a package contains multiple classes ?
    • Solution:-
                       

Steps:-

  1. Create a directory named P.
  2. Store the class A in the file A.java in it.
  3. Compile A.java and place it in the directory P.
  4. Store the class B in the file B.java in it.
  5. Compile B.java and place it in the directory P.
  6. import P.*;will import all classes in the package.



Access Protection for Packages :

This topic is very important and need to learn very carefully, because if it is not maintained properly then either it is a compilition error and you will not be able to build your program successfully. This section needs knowledge of  'Access Modifiers' in JAVA.
Because of the interlay between classes and packages, Java addresses four categories of visibility for class members:
  1. Sub-class in the same package.
  2. Non-sub classes in the same package.
  3. Sub-classes in different packages.
  4. Classes that are neither in the same package nor sub classes.
The three modifiers ie., private, public ans protected,  provide a variety of ways to produce the many levels of access required by these categories.

Rethink :

  • How a package can be accessed in any program ?
  • Is it possible that two classes having the same name but in two different packages are to be used in another class outside the package ?

Post the answers in the comment section below.......................

Let's meet in next Java topics...

Comments