Java Packages

Home   »    Java   »    Java Packages


One of the requirements of an Object Oriented language is that it provides encapsulation. Encapsulation is a technique by which multiple related objects can be grouped under one object. Java implements encapsulation by the use of Packages.

A package is a collection of related classes and interfaces.

Contents



Benefits of Packages

  1. Classes contained in packages of other programs can be easily reused.
  2. Two classes in two different packages can have the same name. They can be uniquely identified by packagename.classname.
  3. Packages also provide a way for separating "design" from "coding".
  4. You can define classes inside a package that are not accessible by code outside that package. You can also define class members that are only exposed to other members of the same package.
In practical applications, we may have to build our own classes and use existing classes libraries for designing use interfaces. Java packages are classified into two types.

Java API package
User defined packages



Using Java API Packages






System Packages

Java API provides a large number of classes grouped into different packages according to functionality. Most of the time in our programming we use the Java System Packages. The following are the frequently used system packages, showing the functional breakdown.



The following are the brief description of Java System Packages and their Classes:

Java.lang Lang means to language. Lang classes are used by java compiler itself and therefore they are automatically imported. Classes that are required for primitive types, strings main functions, threads and exceptions included in it.
Java.utilUtil stands for language utility. It include classes such as vectors, has tables random numbers, date etc.
Java.ioInput/Output support classes are included in it. They provide the facilities for the input and output of data.
Java.awtAwt stands for abstract window toolkit. It is used for implementing graphical user interface in the program. They include classes for windows, buttons, Menu , checkbocx etc.
Java.netNet stands for networking. Therefore the classes required for communicating with local computers as well as with internet swervers are included in it.
Java.appletThis pacake includes the classes for creating and implementing applets.



Creating Packages

To create a package, simply include a package command as the first statement in a Java source file. Any class declared within that file will belong to the specified package. If you omit the package statement, the class names are put into a default package which has no name.

package first_name;
public class firstclass
{
}

The file should be stored as firstclass.java and should be located in a directorynamed first_package. The compiled class file should aslo be stored in the directory that has the same name as the package. Java also supports the concept of package hierarchy. This is done by specifying multiple names in a package statement, separated by dots-package firstpackage.secondpackage;

Store this package in a subdirectory named.

first_package\secondPackage.

Using a Package

Let us consider some simple program that will use classes from other packages. The listing below shows a package named package1 containing a single class. Class Ademo.

Ademo.java

This source file should be named as class Ademo.Java and stored in the subdirectory package1. After compiling this file, a class Ademo.class file will created should be stored in the same subdirectory.

How to compile or make the package use under commands:




after compiling java create a folder named package1 under bin dir....




Now consider the other program as follows:

Test1.java



Output



Download Complete Program

Ademo Test1
  -  
  -  





1 comment: