Search This Blog

Tuesday, 4 October 2011

Creating and Using Packages in Java

0 comments
PACKAGES & INTERFACES!
Packages are nothing but containers like folders that hold one or more classes. A package defines a name space for a set of classes that are related to each other. The name space defined by a package ensures that there are no two classes with the same name. But two packages can have a class with the same name without any name collision among them.
Java uses file system directory to store classes belonging to particular package. For example, the package MyPack will have all its class files in a directory named MyPack. Packages can be nested in a hierarchy i.e., a package can have another package under it.
Classes available in a package can be accessed freely from within the folder that contains it. To make use of them from outside the package, import the package into a Java program using an Import statement. More than one java program can import a package. The following are the steps involved in creating and storing a package:
  1. Include a package command as the first statement in the java file containing class definitions belong to the package.
  2. Create a folder for the package with the same name that of the package (MyPack).
  3. Compile the java files containing classes to get the corresponding class files.
  4. Move the class files from the current folder to the folder having the same name of the package (MyPack).
Defining a Package:
To define a package in a program, simply include a package command as the first statement in a Java source file. Any classes declared after the package command will belong to the specified package.
The package command defines a name space for the classes that are defined after it. The general form of the package statement is:
package pkg;
here, pkg is the name of the package.
An example for a package statement is:
package MyPack;
The following is a C++ program that defines a package named MyPack with two classes: BankAc and BankClient:
package MyPack;
class BankAc{
string name;
double bal;
BankAc(String n, double b)
{
name = n;
bal = b;
}
void Show()
{
System.out.print(“Balance of”);
System.out.println(name + “is” + bal);
}
}
class BankClient{
public static void main(String args[])
{
BankAc sb[] = new BankAc[3];
sb[0] = new BankAc(“Ravi”, 5000.50);
sb[1] = new BankAc(“Raju”, 6000.75);
sb[2] = new BankAc(“Ravi”, 7000.90);


for(int i=0; i<3; i++)
sb[i].Show();
}
}
In this example, two classes are defined for the package MyPack. One is BankAc class that contains two instance variables and two methods for creating and managing a bank account. The second class is an application class named BankClient. After compilation, we will get two class files with the extension .class for the above said classes. These two classes must be stored under the folder created newly with the name MyPack.
Using a Package:
To make use of the classes available in a package from outside the package do the following:
    1. Import the package (MyPack) from a Java program using an import statement containing the package name and
    2. Use the package name and a .(dot) as prefix to the class name that belongs to a Package. For e.g., the BankAc class of MyPack package can be imported as follows:
import MyPack.BankAc;
Nesting a Package:
Nesting a package means having one package defined and stored within another package. Then the package nested becomes a sub package of the main package. To nest a package, specify the name of the parent package(s) and then the sub package, each separated by dot.
The general form of a multilevel statement is shown here:
package pkg1 [pkg2 [pkg3]];
For example, the statement
package java.awt.image;
will create a package with its files stored under the directory
java/awt/image.
Hence, before creating such a package, we should have a directory with the same name and in the same hierarchy as specified in the package statement. Likewise, packages can’t be renamed without renaming the directory in which the classes are stored.
Where to look for Packages?
Importing a package from a Java file will make the run-time system of Java search for the folder containing package. This search starts from the current folder. If it is not found in the current folder, then the path set for Java classes using the command CLASSPATH will be used for searching the package.
Classes stored in a package can be executed from outside the package using the syntax:

java package.classname

For example,
java MyPack.BankAppn
will execute the java class BankAppn.class stored in the package MyPack.
Packages protect Classes:
Packages act as containers for classes and other packages. Classes contain both data and methods (code). Inside the class, we have access specifiers like private and public to have access permission on data and functions at various levels.
The packages, which are at the top level of hierarchy adds another dimension to control the direct access to the classes and its members. Inside a package, a class can be declared with public or private scope. The default scope used for class declaration is private. Only public classes can be accessed for inheritance or object creation from outside the package.


Leave a Reply