Java Tutorial

Java Packages

A package is a collection of classes, interfaces and sub-packages. A sub-package in turns divides into classes, interfaces, sub-sub-packages, etc.

Learning about JAVA is nothing but learning about various packages. By default one predefined package is imported for each and every JAVA program and whose name is java.lang.*.

Whenever we develop any java program, it may contain many number of user defined classes and user defined interfaces. If we are not using any package name to place user defined classes and interfaces, JVM will assume its own package called NONAME package.

In java we have two types of packages they are predefined or built-in or core packages and user or secondary or custom defined packages.

Predefined Packages

Predefined packages are those which are developed by SUN micro systems and supplied as a part of JDK (Java Development Kit) to simplify the task of java programmer.

Note:

Core packages of java starts with java. (For example: java.lang.*) and Advanced packages of java starts with javax. (Forexample: java.sql.*)

Types of predefined packages:

As a part of J2SE we have nine predefined packages which are given in the following table:

Package namePackage description
java.lang.*This package is used for achieving the language functionalities such as convertion of data from string to fundamental data, displaying the result on to the console, obtaining the garbage collector. This is the package which is by default imported for each and every java program.
java.io.*This package is used for developing file handling applications, such as, opening the file in read or write mode, reading or writing the data, etc.
java.awt.*(abstract windowing toolkit)This package is used for developing GUI (Graphic Unit Interface) components such as buttons, check boxes, scroll boxes, etc.
java.awt.event.*Event is the sub package of awt package. This package is used for providing the functionality to GUI components, such as, when button is clicked or when check box is checked, when scroll box is adjusted either vertically or horizontally.
java.applet.*This package is used for developing browser oriented applications. In other words this package is used for developing distributed programs. An applet is a java program which runs in the context of www or browser.
java.net.*This package is used for developing client server applications.
java.util.*This package is used for developing quality or reliable applications in java or J2EE. This package contains various classes and interfaces which improves the performance of J2ME applications. This package is also known as collection framework (collection framework is the standardized mechanism of grouping of similar or different type of objects into single object. This single object is known as collection object).
java.text.*This package is used for formatting date and time on day to day business operations.
java.lang.reflect.*Reflect is the sub package of lang package. This package is basically used to study runtime information about the class or interface. Runtime information represents data members of the class or interface, Constructors of the class, types of methods of the class or interface.
java.sql.*This package is used for retrieving the data from data base and performing various operations on data base.

User Defined Packages

A user defined package is one which is developed by java programmers to simplify the task of the java programmers to keep set of classes, interfaces and sub packages which are commonly used. Any class or interface is commonly used by many java programmers that class or interface must be placed in packages.

Syntax:

package pack1[.pack2[.pack3......packn].....]];

Here, package is a keyword which is used for creating user defined packages, pack1 represents upper package and pack2 to packn represents sub packages.

For example:

package p1; 7 statement-1
package p1.p2; 7 statement-2

The statements 1 and 2 are called package statements.

Rule:

Whenever we create user defined package statement as a part of java program, we must use package statement as a first executable statement.

Note:

Whenever we develop any JAVA program it contains 'n' number of classes and interfaces.

Each and every class and interface which are developed by the programmer must belong to a package (according to industry standards). If the programmer is not keeping the set of classes and interfaces in a package, JVM will assume its own package called NONAME package.

NONAME package will exist only for a limited span of time until the program is completing.

STEPS for developing a PACKAGE:

  1. Choose the appropriate package name, the package name must be a JAVA valid variable name and we showed ensure the package statement must be first executable statement.
  2. Choose the appropriate class name or interface name and whose modifier must be public.
  3. The modifier of Constructors of a class must be public.
  4. The modifier of the methods of class name or interface name must be public.
  5. At any point of time we should place either a class or an interface in a package and give the file name as class name or interface name with extension .java.

For example:

// Test.java package tp;
public class Test
{
    public Test ()
    {
        System.out.println ("TEST - DEFAULT CONSTRUCTOR");
    }
        public void show ()
    {
        System.out.println ("TEST - SHOW");
    }
}

//ITest.java
package tp;
public interface ITest
{
    void disp ();
}

Syntax for compiling a package:

javac -d . filename.java

For example:

javac -d . Test.java

Here, -d is an option or switch which gives an indication to JVM saying that go to Test.java program take the package name and that package name is created as directory automatically provides no errors are present in Test.java. When Test.java is not containing any errors we get Test. class file and it will be copied automatically into current directory which is created recently i.e., tp (package name). The above program cannot be executed since it doesn't contain any main method.

How to use PACKAGE CLASSES and INTERFACES in another java program:

In order to refer package classes and interfaces in JAVA we have two approaches, they are using import statement and using fully qualified name approach.

Using importstatement:

Import is a keyword which is used to import either single class or interface or set of classes and interfaces all at once.

Syntax -1:

Import pack1 [.pack2 [.........[.packn]]].*;

For example:

Import p1.*;	---1
Import p1.p2.*;	---2
Import p1.p2.p3.*; ---3

When statement 1 is executing we can import or we can access all the classes and interfaces of package p1 only but not its sub packages p2 and p3 classes and interfaces.

When statement 2 is executing we can import as the classes and interfaces of package p2 only but not p1 and p3 classes and interfaces.

When statement 3 is executing we can import as the classes and interfaces of package p3 only but not p1 and p2 classes and interfaces.

Syntax-2:

Import pack1 [.pack2 [........[.packn]]].class name/interface name;

For example:

Import p1.c1;	---4
Import p1.p2.c3;	---5

When statement 4 is executing can import c1 class of package p1 only but not other classes and interfaces of p1 package, p2 package and p3 package.

Write a JAVA program which illustrates the usage of package classes?

Answer:

Import approach:
import tp.Test;
class PackDemo
{
    public static void main (String [] args)
    {
        Test t1=new Test ();
        t1.show ();
    }
};

When we compile the above program we get the following error "package tp does not exist". To avoid the above error we must set the classpath as., SET CLASSPATH = %CLASSPATH%;.;

This is the alternate technique for import statement:

p1.c2 o2=new p1.c2 ();
p1.p2.p3.c4 o4=new p1.p2.p3.c4 ();
p1.p2.i3 o3=new p1.p2.p3.c4 ();

Fully qualified approach:

class PackDemo
{
    public static void main (String [] args)
    {
        tp.Test t1=new tp.Test ();
        t1.show ();
    }
};

Note:

  1. Whenever we develop user defined packages, to use the classes and interfaces of user defined packages in some other program, we must set classpath before there usage.
  2. In order to set the classpath for predefined packages we must use the following statement: D:\core\set classpath=C: \Program Files\Java\jdk1.5.0\lib\rt.jar;.; [rt.jar contains all the .class files for the predefined classes which are supplied as a port of predefined packages by the SUN micro systems.]

When two classes or an interface belongs to the same package and if they want to refer those classes or interfaces need not to be referred by its package name.

For example:

// I1.java
// javac -d . I1.java

package ip;
public interface I1
{
    public void f1 ();
    public void f2 ();
};

// C01.java
// javac -d . C01.java

package cp;
public abstract class C01 implements ip.I1
{
    public void f1 ()
    {
        System.out.println ("F1 - C01");
    }
};

// C02.java
// javac -d . C02.java

package cp;
public class C02 extends C01
{
    public void f2 ()
    {
        System.out.println ("F2 - C02");
    }
    public void f1 ()
    {
        super. f1 ();
        System.out.println ("F1 - C02 - OVER RIDDEN");
    }
};

// PackDemo.java
// javac PackDemo.java

import ip.I1; class PackDemo
{
    public static void main (String [] args)
    {
        System.out.println ("CP.C02");
        cp.C02 o2=new cp.C02 ();
        o2.f1 ();
        o2.f2 ();
        System.out.println ("CP.C01");
        cp.C01 o1=new cp.C02 ();
        o1.f1 ();
        o1.f2 ();
        I1 io;
        io=o1; //new cp.C02 ();
        io.f1 ();
        io.f2 ();
    }
};

In order to run the above program we must run with respect to package name.

1 javac -d . PackDemo1.java

2 java mp.PackDemo1 or java mp/PackDemo1