Package in
Package in
Package in
Introduction
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
Packages are used for:
• Preventing naming conflicts. For example there can be two classes with name Employee in
two packages, college.staff.cse.Employee and college.staff.ee.Employee
• Making searching/locating and usage of classes, interfaces, enumerations and annotations
easier
• Providing controlled access: protected and default have package level access control. A
protected member is accessible by classes in the same package and its subclasses. A default
member (without any access specifier) is accessible by classes in the same package only.
• Packages can be considered as data encapsulation (or data-hiding).
All we need to do is put related classes into packages. After that, we can simply write an import
class from existing packages and use it in our program. A package is a container of a group of
related classes where some of the classes are accessible are exposed and others are kept for
internal purpose.
We can reuse existing classes from the packages as many time as we need it in our program.
Java Packages are classified into two types. First is known as Java API packages and second is
known as user defined packsges.
Java APl(Application Program Interface) provides a large numbers of classes grouped into
different packages according to functionality. Most of the time we use the packages available with
the the Java API. Following figure shows the system packages that are frequently used in the
programs.
Java System Packages and Their Classes
java.lang Language support classes. They include classes for primitive types, string, math functions, thread
and exceptions.
java.util Language utility classes such as vectors, hash tables, random numbers, data, etc.
java.io Input/output support classes. They provide facilities for the input and output of data.
java.net Classes for networking. They include classes for communicating with local computers as well as
with internet servers.
java.awt Set of classes for implementing graphical user interface. They include classes for windows,
buttons, lists, menus and so on.
Color
Graphics
Font
.
.
.
Image
Naming Conventions
Creating Packages
To create our own package , we must first declare the name of package using the package
keyword followed by a package name. This must be first statement in a java source file.
Then we define a class , just as we normally defined a class.
Ex.
Package firstpackage;
Public class FirstClass
{
…….
……. ( Body of class )
}
Here package name is firstPackage.
The class FirstClass is now considered a part of this package.
This file is saved as FirstClass.java and located in a directory named firstPackage. When the
source file is compiled , java will create a .class file and store it in the same directory.
To create our own package following steps are there.
1. Declare the package at the beginning of a file using the form
package packagename;
2. Define the class that is to be put in the package and declare it public.
3. Create a subdirectory under the directory where the main source files are stored.
4. Store the programs as classname.java file in the subdirectory created.
5. Compile the file . This creates .class file in subdirectory.
Remember that name of the package must be same as the directory under which this file is saved.
Accessing a Package
Using a Package
The package p1 contains one public class by name A. Suppose we want to add another class B 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 p1
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.
Now , the package p1 will contain both the classes A and B . The statement like
import p1. * ;
will import both of them.
***********