PACKAGES
Java API provides a large number of classes grouped into different
packages according to functionality. Most frequently used packages are:-
- Java.lang:
These are classes that java
compiler itself uses and therefore they are automatically imported. They
include classes for primitive types.
- Java.util:
A collection of classes to
provide utility functions such as vectors, hash tables, random numbers, date
and time functions etc.
- Java.io:
A Collection of classes required
for Input/Output manipulations.
- Java.net:
A collection of classes for
communicating with other computers via network.
- Java.awt:
The Abstract Window Tool Kit
package contains classes that implements platform independent graphical user
interface.
- Java.applet:
This includes a set of classes
that allows us to create Java applets.
Using system Packages
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, if we want
to refer to the class Color in the awt package, then we may do so as follows:
java.awt.Colour
Notice
that awt is a package within the package java and the hierarchy is represented
by separating the levels with dots. This approach is perhaps the best and
easiest one if we need to access the class only once or when we need not have
to access any other classes of the package.
But,
in many situations, we might want to use a class in a number of places in the
program or we may like to use many of the classes contained in a package. We
may achieve this easily as follows;
import
packagename.classname;
or
import
packagename.*;
These
are known as import statements and must appear at the top of the file,
before any class declarations.
The
first statement allows the specified class in the specified package to be
imported. For example, the statement
import java.awt.Color;
imports
the class Colour and therefore the class name can now be directly used in the
program.
The
second statement imports-every class contained in the specified package. For
example, the statement import java.awt.*; bring all classes of java.awt package
Naming Conventions
- Packages
begin with lowercase letters
- Every
package name must be unique
Creating Packages
Packages declare the name of the package using the package keyword
followed by a package name. This must be the first statement in Java source
file.
public
class firstclass
{
body
}
here the package name is first. This should be saved as firstclass.java
and located in a directory named first. When the source file is compiled, java
will create a .class file and store it in the same directory.
Steps to
create package
- 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 file as the classname.java in the subdirectory created.
- Compile
the file. This creates .class file in the subdirectory.
Subdirectory
name must match the package name exactly. Java also supports the concept of
package hierarchy.
A
java package file can have more than one class definition. In such cases, only
one of the classes may be declared as public and that class name with .java
extension is the source file name. When a source file with more than one class
definition is compiled, Java creates independent .class files for those
classes.
Accessing a
Package
Packages
are accessed through a fully qualified class name or using a shortcut approach
through the import statement. We use the import statement when there are many
references to a particular package or the package name is too long and
unwieldy.
The
same approaches can be used to access the user-defined packages as well. The
import statement can be used to search a list of packages for a particular
class. The general form of import statement for searching a class is as
follows:
import
packagel [.package2][.package3] .classname;
Here
packagel is the name of the top level package, package2 is the
name of the package that is inside the packagel, and so on. We can have
any number of packages in a package hierarchy. Finally, the explicit class
name is specified.
The
following is an example of importing a particular class:
import
firstPackage.secondPackage.MyClass;
After
defining this statement, all the members of the class MyClass can be directly
accessed using the class name or its objects directly without using the package
name.
We
can also use another approach as follows:
import packagename. *;
Here,
packagename may denote a single package or a hierarchy of packages as
mentioned earlier. The star (") indicates that the compiler should search
this entire package hierarchy when it encounters a class name. This implies
that we can access all classes contained in the above package directly.
Using a Package
This
source file should be named calculations.java and stored in the subdirectory
arithmetic. Now compile this java file. The resultant calculations.class will
be stored in the arithmetic subdirectory.
//Program to calculate arithmetic operations
package arithmetic;
public class calculations
{
public int
add(int a,int b)
{
return
a+b;
}
public int sub(int
a,int b)
{
return
a-b;
}
public int
mul(int a,int b)
{
return
a*b;
}
public int
div(int a,int b)
{
return
a/b;
}
}
Now
consider the listing shown below
//importing package
import arithmetic.calculations;
class performoperations
{
public static
void main(String []args)
{
calculations
c=new calculations();
int
a=Integer.parseInt(args[0]);
int
b=Integer.parseInt(args[1]);
int
z=c.mul(a,b);
int
v=c.sub(a,b);
System.out.println("Addition
is:"+c.add(a,b));
System.out.println("Division
is:"+c.div(a,b));
System.out.println("Multiplication
is:"+z);
System.out.println("Subtraction
is:"+v);
}
}
Output:
D:\ \programs>javac performoperations.java
D:\ \programs>java performoperations 6 2
Addition is:8
Division is:3
Multiplication is:12
Subtraction is:4
This
listing shows a simple program that imports the class calculations from the
package arithmetic. The source file should be saved as performoperations.java
and then compiled.
During
the compilation of performoperations.java the compiler checks for the file
calculations.class in the arithmetic directory for information it needs, but it
does not actually include the code from calculations.class in the file
performoperations.java. When the performoperations.java program is run, Java
looks for the file calculations.class and loads it using something called class
loader. Now the interpreter knows that it also needs the code in the file
calculations.class and loads it as well.
Adding a Class to a Package
It
is simple to add a class to an existing package. Consider the following
package:
package
pl;
public
ClassA
{
//
body of A
}
The
package p1 contains one public class by name A. Suppose we want to add another
class to this package. This can be done as follows:
1.
Define the class and make it public.
2.
Place the package statement
package
p1;
before
the class definition as follows:
package
pl;
public
class B
{
// body of B
}
3.
Store this as B.java file under the
directory p1.
4. Compile B.java file. This will create a B.class file and place it
in the directory p1.
Remember
that, since a Java source file can have only one class declared as public, we
cannot put two or more public classes together in a .java file. This is because
of the restriction that file name should be same as the name of the public
class with .java extension.
If
we want to create a package with multiple public classes in it, we may follow
the following steps:
- Decide 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;
at the top of each source file.
- Switch to the
subdirectory created earlier and compile each source file. When completed,
the package would contain. class files of the entire source files.
Hiding Classes
When we import a package using asterisk(*), all public classes are
imported. However, we may prefer to not import certain classes. Such classes
should not be declared as public.
Ex:
package p1;
class y //not public so it
is hidden class
{
body
}
Here
the class y which is not declared public is hidden from outside of package p1.
No comments:
Post a Comment