You are on page 1of 16

Packages

One advantage of OOP is reusability. Reusability can be achieved by using classes from other programs without physically copying them into the program under development. It can be achieved by using pacakges which is similar to class libraries. Packages allow us to group variety of classes and interfaces together.

Benefits of packages
Classes in the packages of other programs can be easily reused. Two classes in two different packages can have same name. They may be referred by their fully qualified name. We can hide classes preventing other programs or packages from accessing classes that are meant for internal use only. Seperates design from coding.

Packages help organize files within a program, as well as to collect objects into groups to improve reusability of code
A package structure is a tree structure that maps to a folder/directory structure package names correspond to directory names a dot is used between levels of a multilevel structure searching for files starts at the root level of the structure during compiling or class-load at runtime you can't use packages or directory structures without explicitly placing a class in a package via your code classes are always referenced from the root of the structure, and the fully-qualified name of the class contains the relative path elements to reach the class from the root point, separated by dot characters

Classification of Packages
Java API packages. User defined packages.

Creating packages
The accepted convention is that package names be all lowercase. package test; public class XYZ { . . . } XYZ.java should be in a directory called test that is located within a directory listed in our CLASSPATH the fully-qualified name of the class is test.XYZ

To run an executable Java class that belongs to a package, your command prompt should be at the directory level that matches the root of the package structure you would then treat the class name as packagename.ClassName Example - the directory C:\MyProject is the root level of a package structure, and the main class, XYZ.class, is in the package called test (therefore test is a subdirectory of MyProject)

To run XYZ.class, the command line would look like the following: C:\MyProject>java test.XYZ

Accessing classes in packages


To use fully qualified class name. Can be done by using the package name containing the class and then appending the class name to it by using the dot operator e.g. java.awt.Color. This approach is useful when we want to use class only once. When we want to use classes at multiple places in the program it can be achieved by importing the package e.g. import java.awt.Color

Creating packages
Declare the package at the beginning of a file using the form package packagename; Define the class that is to be put in the package and declare it public. Create a subdirectory under the directory where the main source files are stored. Store the listing as the classname.java file in the subdirectory created. Compile the file. This creates .class file in the subdirectory.

Adding a classes to a package


Declare the name of the package. Create a subdirectory with this name under the directory where main source files are stored. Create classes that are to be placed in the package in separate source files and declare the package statement
Package packagename;

Switch to the subdirectory created earlier and compile each source file. Hiding of classes can be done by not declaring the classes public.

You might also like