Thursday, July 25, 2013

Programming In Java: Packages

Packages

Packages are containers for classes that are used to keep the class name space compartmentalized. For example, a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other class named List stored elsewhere. Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.

Defining a Package

To create a package, simply include a package command as the first statement in a Java source file. Any classes declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored. If you omit the package statement, the class names are put into the default package, which has no name.
This is the general form of the package statement:
package pkg;
Here, pkg is the name of the package.
For example, the following statement creates a package called ihrd.
package ihrd;
Java uses file system directories to store packages. For example, the .class files for any classes you declare to be part of ihrd must be stored in a directory called ihrd. Also, more than one file can include the same package statement.
We can also create a hierarchy of packages. To do so, simply separate each package name from the one above it by use of a period. The general form of a multileveled package statement is shown here:
package pkg1[.pkg2[.pkg3]];
A package hierarchy must be reflected in the file system of your Java development system. For example, a package declared as
package java.awt.image;

needs to be stored in folder java\awt\image

Example:
This program has to be saved as AccountBalance.java inside folder pkg.

package pkg;
class Balance{
String name;
double bal;
Balance(String n, double b){
name = n;
bal = b;
}
void show(){
System.out.println(name + " : " + bal);
}
}

class AccountBalance{
public static void main(String[] args){
Balance[] b = new Balance[3];
b[0] = new Balance("ABCD", 100.00);
b[1] = new Balance("PQRS", 1000.00);
b[2] = new Balance("XYZ", 10000.00);

for(int i=0; i<3; i++)
b[i].show();

}

}
Compiling the program: javac pkg\AccountBalance.java  (from the parent folder of pkg)
Running the program:    java pkg.AccountBalance

Output
ABCD : 100.00
PQRS  : 1000.00
XYZ : 10000.00

Access Protection

Classes and packages are both means of encapsulating and containing the name space and scope of variables and methods. Packages act as containers for classes and other subordinate packages. Classes act as containers for data and code. Because of the interplay between classes and packages, Java addresses four categories of visibility for class members:
·        Subclasses in the same package
·        Non-subclasses in the same package
·        Subclasses in different packages
·        Classes that are neither in the same package nor subclasses
The three access specifiers, privatepublic, and protected, provide a variety of ways to produce the many levels of access required by these categories. Table below sums up the interactions.


Private
No Modifier
Protected
Public
Same class
Yes
Yes
Yes
Yes
Same Package subclass
No
Yes
Yes
Yes
Same package non-subclass
No
Yes
Yes
Yes
Different package subclass
No
No
Yes
Yes
Different package non-subclass
No
No
No
Yes
While Java’s access control mechanism may seem complicated, we can simplify it as follows. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member does not have an explicit access specification, it is visible to subclasses as well as to other classes in the same package. This is the default access. If you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare that element protected.

Importing Packages

Given that packages exist and are a good mechanism for compartmentalizing diverse classes from each other, it is easy to see why all of the built-in Java classes are stored in packages. There are no core Java classes in the unnamed default package; all of the standard classes are stored in some named package. Since classes within packages must be fully qualified with their package name or names, it could become tedious to type in the long dot-separated package path name for every class you want to use. For this reason, 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.
In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions. This is the general form of the import statement:
import pkg1[.pkg2].(classname|*);
Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package inside the outer package separated by a dot (.).Finally, you specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package. This code fragment shows both forms in use:
import java.util.Date;
import java.io.*;
All of the standard Java classes included with Java are stored in a package called java. The basic language functions are stored in a package inside of the java package called java.lang. Normally, you have to import every package or class that you want to use, but since Java is useless without much of the functionality in java.lang, it is implicitly imported by the compiler for all programs. This is equivalent to the following line being at the top of all of your programs:
import java.lang.*;
It must be emphasized that the import statement is optional. Any place you use a class name, you can use its fully qualified name, which includes its full package hierarchy. For example, this fragment uses an import statement:
import java.util.*;
class MyDate extends Date {
}
The same example without the import statement looks like this:
class MyDate extends java.util.Date {
}
In this version, Date is fully-qualified.

No comments:

Post a Comment