Friday, August 22, 2014

Packages.

Programs are organized as sets of packages. Each package has its own set of names for types, which helps to prevent name conflicts. A top level type is accessible outside the package that declares it only if the type is declared public.

The naming structure for packages is hierarchical. The members of a package are class and interface types, which are declared in compilation units of the package, and subpackages, which may contain compilation units and subpackages of their own.

A package can be stored in a file system or in a database. Packages that are stored in a file system may have certain constraints on the organization of their compilation units to allow a simple implementation to find classes easily.

Package members:
  • The package java has subpackages awt, applet, io, lang, net, and util, but no
compilation units.
  • The package java.awt has a subpackage named image, as well as a number of
compilation units containing declarations of class and interface types.

Host support for packages:
Each host system  deter mains how packages and compilation units are created and stored.
Each host system also determines which compilation units are observable in a particular. The observability of compilation units in turn determines which packages are observable, and which packages are in scope.

Compilation units:
java.lang are always observable.
import allow types from other packages and members of type to be refered using their simple name.

Package declaration.
A package declaratoin appears within a compilation unit to indicate the package to which the compilation unit belongs.

Observability of backage:
A compilation unit containing a declaration of the package is observable. subpackage of the package is observable

One can conclude this from the rule above and from the rules of observable compilation units, as follows. The predefined package java.lang declares the class Object, so the compilation unit for Object is always observable. Hence, the java.lang package is observable, and the java package also. Furthermore, since Object is observable, the array type Object[] implicitly exists. Its superinterface java.io.Serializable also exists, hence the java.io package is observable

package test;
import java.util.Vector;
class Point {
 int x, y;
}
interface Point { // compile-time error #1
 int getR();
 int getTheta();
}
class Vector { Point[] pts; } // compile-time error #2
----------------------- -------------------------- --------------------
class Point {
  int x, y; // coordinates
  PointColor color; // color of this point
  Point next; // next point with this color
  static int nPoints;
}

class PointColor {
  Point first; // first point with this color
  PointColor(int color) { this.color = color; }
  private int color; // color components
}

No comments:

Post a Comment