Packages
are Java’s way of grouping a variety of classes and/or interfaces together. The
grouping is usually done according to functionality.
Packages
act as “Container” for classes.
Packages
provides the following benefits:
o The
classes contained in the packages of other programs can be easily reused.
o In
packages, classes can be unique compared with other classes in other packages.
That is, two classes in two different packages can have same name. They may be
referred by their fully qualified name, comprising the package name and the
class name.
o Packages
provide a way to hide classes and thus preventing other program or packages
from accessing classes that are meant for internal use only.
o Packages
also provide a way for separating “design” from “coding”. First we can design
classes and decide their relationships, and then we can implement the Java code
needed for the methods.
Java
packages are classified into two catagories: Java API packages,
o Java API
Packages: Java API provides a large number of
classes grouped into different packages according to functionality. The
following table shows the classes that belong to each package:
Package Name
|
Contents
|
java.lang
|
Language support classes. These are
classes that Java compiler itself uses and therefore they are autometically
imported. They include classes for primitive types, strings, math functions,
threads and exceptions.
|
java.util
|
Language utility classes such as
vectors, hash tables, random numbers, date, etc.
|
java.io
|
Imput/Output support classes. They
provide facilities for the input and output of data.
|
java.awt
|
Set of classes for implementing
graphical user interface. They include classes for windows, button, list,
menus and so on.
|
java.net
|
Classes for networking. They include
classes for communicating with local computers as well as with internet
server.
|
java.applet
|
Classes for creating and implementing
applets.
|
o
Using
System Packages: There are two ways to access the
classes stored in a package:
First,
Use the fully qualified class name of the class that we want to use. Like, to
refer to class Color in the awt package, the following command is
used:
java.awt.Colour
This approach is useful if one
need to access the class only once or when it is not required to access any
other classes of the package.
Second method is to use the import statementand must appear at the
top of the file, before any class declaration. It should be used in the
following way:
import packagename.classname;
or
import packagename.*;
Ex:
import java.awt.Color;
import java.awt.*; //bring all classes of java.awt package
o
Naming
Conventions: double y=java.lang.Math.sqrt(x);
package class method
name name
name
o
Creating
Packages:The steps that are followed for creating
our own packages are as follows:
§ 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.
§ The
name of the subdirectory must match the package name exactly.
§ Java
also supports the concept of package hierarchy. This is done by specifying
multiple names in a package statement, separated by dots.
package
firstpackage.secondpackage;
§ A
Java package file can have more than one class definition. In such a case, only
one of the classes may be declared 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.
o
Using
a Package:
Ex:Define a package:
package
Mypack;
public
class Display
{
public void show()
{
System.out.println("\nShow method in
package Mypack in display is called");
}
}
class
Design
{
void show()
{
System.out.println("\nShow method in
package Mypack in design is called");
}
}
Define a class:
import
Mypack.*;
class
Package{
public static void main(String args[])
{
Display d=new Display();
d.show();
//Design
d1=new Design();
//d1.show();
//
Design is not public hence can’t be accessed from outside of package.
}
}
No comments:
Write comments