Packages
Introduction
Most programming languages provide ready made code libraries that contain a
group of related files. By using those contents of these libraries, we can
considerably save coding time and effort. The files under these code libraries are
sorted and arranged according to their functionality. In different languages they are
known by different names. In C they are called header files, in C++ class libraries
and in Java they are called Packages.
A package is a combination of classes, interfaces and sub packages. In fact
packages act as containers for class.
The main feature of OOP is its reusability. One way achieving this is by
extending the classes and implementing the interfaces. Another way of achieving
the reusability in Java is to use packages.
Advantages of packages
The classes contained in the packages of other programs can be easily
reused.
In packages, classes can be unique compared with classes in other packages
that is two classes in two different packages can have the same name.
Packages can provide a way to hide classes, thus preventing other program
of packages from accessing classes that are meant for internal use only.
Packages also provide a way for separating design from coding.
Java packages are therefore classified into two types
1. Java API packages.
2. User defined packages
Java API packages
Java API provides a large number of classes grouped into different packages
according to functionality. The frequently used java API packages are
java
appl
lang util io awt net
et
java.lang
o This package contains utility classes such as vectors, date, random etc.
java.util
o This package contains input/output support classes. They provide
facilities for the input and output of date.
java.io
o This package contains input/output support classes. They provide
facilities for the input and output of date.
java.awt
o This package contains a set of classes for implementing graphical user
interface. They provide facilities for the input and output of date.
java.net
o This package contains classes for networking. They include classes for
communicating with local computers as well as with internet servers.
java.applet
o This package contains classes for creating and implementing applets.
Using system packages:
This packages are organized in a hierarchial structure. There are two ways of
accessing the classes stored in a package. The first approach is to use the fully
qualified class name of the class that we want to use. This is done by using the
package name containing the class and then appending the class name to it using
the dot operator.
For example: java.io.DataInputStream
Naming conventions
Packages can be named using the standard java naming rules. By
conventions, however, packages begin with lowercase letters. This makes it easy for
users to distinguish packages begin with lowercase letters. This makes it easy for
users to distinguish package names from class names when looking at an explicit
reference to a class.
Every package name must be unique to make the best use of packages.
Duplicate names will cause run-time errors.
Creating packages(user defined packages)
Java language provides facility for creating our own packages. These
packages are called user-defined packages.
Creating packages
To create a user defined packages steps are as follows
Method1
1) Declare the package at the beginning of a file using the package keyword
followed by a package name. This must be first statement in java source
file(except for comments)
Syntax: package my package;
Ex: siva
2) Import the required standard packages available using the import statement.
Ex: import java.util.*;
3) Declare and define the classes that will be included in the package. All the
members of the package should be public to be accessed from outside.
Ex: package mypackage;
Import java.util.*;
Public class Math
{
// class definition
}
4) Save the above definition in a java file and compile the classes defined in the
package. Compilation can be done with a -d option. This creates a folder
with the package name and places the .class file into the specified folder.
Javac d c:\java math.java
Method2
1) Create a subdirectory where the main source files are stored, the directory
name must match the package name exactly.
2) Store the listing as the classname.java file in the subdirectory created.
3) Compile the file. This creates .class file in the subdirectory.
Accessing a package
Java includes import statement to bring certain classes or entire package
accessability.
The import statement can be used to search a list of packages for a particular class.
In java source file,import statements occur immediately following the
package statement(if it exists) and before any class definitions.
The general form of the import statement is
Import package1 [package2].classname;
Here
Package1 is the name of the top-level package.
Package2 is the name of the package that is inside package1.
The outer package separated by dot.
classname/* which indicates that the java compiler should import the specified
class or a package.
import statement should end with semicolon.
Ex: import java.awt.colour; //importing system packages.
Import java.awt.*;
Import pack1.ClassName; //importing user defined packages
Where pack1 is the name of the user defined package when we import a package
using astrick(*) all public classes are imported. However we may refer to not
import certain outside of the package. Such classes should be declared not public.
CLASSPATH
While packages solve many problems from an access control and name space
collision prespective, they cause some curious difficulties. When you compile and
run programs. This is because the specific location that the java compilers will
consider as the root of any package hierarchy is controlled by CLASSPATH.
When java program is executed, the JVM searches for the classes used within
the program on the file system. It uses one of two elements to find a specific class.
1. The package name and
2. The directories listed in the CLASSPATH environment variable. If no
CLASSPATH is defined, then JVM looks for the default java\lib directly and the
current working directory.
The different access modifiers and its access locations as shown below while using
packages and inheritance in a program
Public Protecte Friendly(defau Private priva
d lt) Protected te
Same class Yes Yes Yes Yes Yes
Sub class in the same Yes Yes Yes Yes No
package
Other classes in same Yes Yes Yes No No
package
Subclasses in other Yes Yes No Yes No
packages
Non subclasses in other Yes No No No No
packages
/*user defines package demonstration program*/
//creating arith class as a member in math package
Package math; //package statement
Public class Arith
Public int add(int x, int y)
Return(x+y);
Public int sub(int x,int y)
Return(x-y);
Public int mul(int x, int y)
Return(x*y);
Public int div(int x, int y)
Return(x/y);
//save as arith.java
//javac d E:\java Arith.java
//creating stat class as a member in math package
Package math; //package statement
Public class stat
{
Public int max(int x, int y)
Return(x>y?x:y);
Public int min(int x, int y)
Return(x<y?x:y);
//save as Stat.java
//javac d E:\java Stat.java
/*package testing program*/
Import math.Arith; //importing Arith class from math package
Import math.Stat; //importing Stat class from math package
Import java.io.*;
Class Pack Test
Public static void main(String args[])
Arith a =new Arith(); //creating an instance of Arith class
System.out.println(addition :+a.add(10,20));
System.out.println(substraction:+a.sub(20,10));
System.out.println(multiplication+a.mul(10,20));
System.out.println(division:+a.div(20,10));
Stat s=new Stat(); //creating an instance of Stat class
System.out.println(maximum value:+s.max(10,20));
System.out.println(minimum value:+s.min(10,20));
//execution procedure
>set class path= ; E:/java; //setting class path
>javac PackTest.java //compilation
>java PackTest //run
Adding a class to a package
It is simple to add a class to an existing package. For example
Package p1;
Public class A
//body of class A\
In the above example, the package p1 contains one public class by name A.
Suppose we want to add class B to this package. This can be done as follows:
Define the class and make it public.
Place the package statement before the class definition as follows:
Package p1;
Public class B
{
//body of class B
}
Store
Compile
Hiding classes
When we import a package using asterisk(*), all public classes are imported.
If you want to hide these classes from accessing from outside of the package, such
classes should be declared not public.
For example
Package p1;
Public class
//body of class
Class Y
//body of class Y
Here, the class Y which is not declared public is hidden from outside of the
package p1. This class can be seen and used only by other classes in the same
package.
Static import
Static import is another feature introduced with the J2SE 5.0 release. This
feature eliminates the need of qualifying a static member with class name. We can
use the static import statement to import static members from classes and use
them without qualifying class name.
The syntax for static import feature is
Import static packagename.subpackagename.classname.static membername.
Import static packagename.subpackagename.classname.*;
Public class MathDemo
Public void MathDemo
Double area=PI*r*r;
System.out.println(the area of circle is:+area);
}
Public static void main(String args[])
MathDemo mt=new MathDemo();
Mt.circle(3.4);