18-04-2012, 02:26 PM
OBJECT ORIENTED PROGRAMMING
UNIT-4.ppt (Size: 166.5 KB / Downloads: 49)
Defining a Package
A package is both a naming and a visibility control mechanism:
1) divides the name space into disjoint subsets It is possible to define classes within a package that are not accessible by code outside the package.
2) controls the visibility of classes and their members It is possible to define class members that are only exposed to other members of the same package.
Same-package classes may have an intimate knowledge of each other, but not expose that knowledge to other packages
Packages and Directories
Java uses file system directories to store packages.
Consider the Java source file:
package myPackage;
class MyClass1 { … }
class MyClass2 { … }
The byte code files MyClass1.class and MyClass2.class must be stored in a directory myPackage.
Case is significant! Directory names must match package names exactly.
Package Hierarchy
To create a package hierarchy, separate each package name with a dot:
package myPackage1.myPackage2.myPackage3;
A package hierarchy must be stored accordingly in the file system:
1) Unix myPackage1/myPackage2/myPackage3
2) Windows myPackage1\myPackage2\myPackage3
3) Macintosh myPackage1:myPackage2:myPackage3
You cannot rename a package without renaming its directory!
Accessing a Package
As packages are stored in directories, how does the Java run-time system know where to look for packages?
Two ways:
1) The current directory is the default start point - if packages are stored in the current directory or sub-directories, they will be found.
2) Specify a directory path or paths by setting the CLASSPATH environment variable.
UNIT-4.ppt (Size: 166.5 KB / Downloads: 49)
Defining a Package
A package is both a naming and a visibility control mechanism:
1) divides the name space into disjoint subsets It is possible to define classes within a package that are not accessible by code outside the package.
2) controls the visibility of classes and their members It is possible to define class members that are only exposed to other members of the same package.
Same-package classes may have an intimate knowledge of each other, but not expose that knowledge to other packages
Packages and Directories
Java uses file system directories to store packages.
Consider the Java source file:
package myPackage;
class MyClass1 { … }
class MyClass2 { … }
The byte code files MyClass1.class and MyClass2.class must be stored in a directory myPackage.
Case is significant! Directory names must match package names exactly.
Package Hierarchy
To create a package hierarchy, separate each package name with a dot:
package myPackage1.myPackage2.myPackage3;
A package hierarchy must be stored accordingly in the file system:
1) Unix myPackage1/myPackage2/myPackage3
2) Windows myPackage1\myPackage2\myPackage3
3) Macintosh myPackage1:myPackage2:myPackage3
You cannot rename a package without renaming its directory!
Accessing a Package
As packages are stored in directories, how does the Java run-time system know where to look for packages?
Two ways:
1) The current directory is the default start point - if packages are stored in the current directory or sub-directories, they will be found.
2) Specify a directory path or paths by setting the CLASSPATH environment variable.