Java-Packages

Packages

One shall reuse the classes and interfaces in a program by extending or implementing them in that program Sometimes, we may wish to reuse the classes and interfaces in a program within another program. One way of achieving this is to physically copy the classes and interfaces in a program into another program and then reuse them. This is obviously a laborious process. The better alternative strategy is to make use of the concept of packages.

A package is a collection of related classes and interfaces. We can easily access the classes and interfaces in a package in a program by importing that package into that program. 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 “containers” for classes.

Package 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.

Creating Package

We must first declare the name of the package using the package keyword followed by a package name. This must be the first statement in a Java source file. Then we define a public class, just as we normally define a class. Here is an example :

 

Here the package name is mypack. The class MyClass is now considered a part of this package. This listing would be saved as a file called MyClass.java, and located in a directory named mypack. When the source file is compiled, Java will create a MyClass.class file and store it in the same directory. Remember that the .class files must be located in a directory that has the same name as the package, and this directory should be a subdirectory of the directory where classes that will import the package are located.

Accessing Package

Java includes the import statement to bring certain classes, or entire packages, into visibility. Once imported, a class can be referred to directly, using only its name. The import statement is a convenience to the programmer and is not technically needed to write a complete Java program. The example of import statement as follows :

 

Here mypack is the name of the package, MyClass is the name of the class that is inside the mypack and is represented by separating the dots. Note that the statement must end with a semicolon (;). The import statement should appear before any class definitions in a source file.

Program

final

Remember that the package .class files must be located in a directory that has the same name as the package, and this directory should be a subdirectory of the directory where classes that will import the package are located.

Package Source

package mypack;

public class MyClass {
    
    public void display() {
        
        System.out.println("MyClass");
        
    }
}

Program Source

import mypack.MyClass;

public class Javaapp {
    
    public static void main(String[] args) {
        
        MyClass my = new MyClass();
        my.display();
    }
}

Leave a Comment