0% found this document useful (0 votes)
11 views14 pages

5 Packages

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

5 Packages

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Packages: Putting classes

together
Introduction
• A way to achieve reusability in Java
• A concept similar to ‘class libraries’ in other
languages.
• Packages are Java’s way of grouping a variety
of classes and/or interfaces together.
• Grouping is usually done according to
functionality.
• Packages act as ‘containers’ for classes.
Benefits of using packages
• The classes contained in the packages of other programs
can be easily reused.
• Two classes in two different packages can have the same
name. They may be referred by their fully qualified name,
comprising the package name and the class name.
• Packages provide a way to ‘hide’ classes thus preventing
other programs or packages from accessing classes that
are meant for internal use only.
• Packages provide a way for separating ‘design’ from
‘coding’.
Classifications of Java packages
• Java packages are classified into two types:
– Java API packages
– User defined packages
JAVA API PACKAGES
• Java API(Application Programming Interface)
provides a large number of classes grouped
into different packages according to
functionality.
Frequently used API packages
STAND ALONE APPLICATION BASED PACKAGE

Package Name Purpose


1. java.lang This is a language support classes. These classes the java
compiler itself uses therefore they are automatically imported.
This is a language utility classes such as random numbers,
2. java.util
date, vectors etc.
This is Input/output support classes. This class provides the
3. java.io
facilities for the input and output of data.
WEB BASED APPLICATION BASED PACKAGE

Package Name Purpose


AWT stands for abstract window toolkit. This is a Set of classes
1. java.awt for implementing graphical user interface. They are having
classes for windows, buttons, lists, menus etc.
This Class represents networking. They are having classes for
2. java.net communicating with local computers as well as with internet
servers.
3. java.applet This Class for creating and implementing applets.
Using System Package
• Packages are organized in a hierarchical
structure.

Fig: Hierarchical representation of java.awt package


• Package named java contains the package awt, which in turn contains various
classes required for implementing graphical user interface.
• Generally there are two process 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 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 (Abstract Window
Toolkit) package, then we can write as
java.awt.Color
• In many situations we might want to use a class in number of places in the
program or we may like to use many of this class contain in a package. We can do
this easily as following methods.
import packagename.classname;
or
import packagename.*;
• These are also known as import statements and must appear at the top of the file
before any class declaration.
• import java.awt.Color;
imports the class Color and therefore the class name can now be directly used in
the program.There is no need to use the package name to qualify the class.
• import java.awt.*;
imports every class contained in the java.awt package.
NAMING CONVENTIONS
• Packages can be named using the standard Java naming rules.
• By convention packages begin with lower case letters.
• The entire class name begins with upper case letters.

This statement uses a fully qualified class name Math to invoke the method pow().
• java.awt.Point pts[ ]

This statement declares an array of Point type objects using the fully
qualified class name.
CREATING PACKAGES
• First declare the name of the package using the package keyword followed by
a package name.
• This must be the first statement in a Java source file(except for comments
and white spaces)
• eg:
package firstPackage; //package declaration
public class FirstClass //class definition
{
body of class
}
• Save this as a file called FirstClass.java, 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.
Creating our own package involves the following steps:
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 listing as the classname.java file in the subdirectory created.
5. Compile the file. This creates .class file in the subdirectory.
• A java package file can have more than one class definitions. In such
cases, 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.
Using a package
• package package1;
public class ClassA
{
public void displayA()
{
System.out.println(“Class A”);
}
}
• This source file should be named classA.java and stored in the subdirectory package1.
Compile this java file. The resultant ClassA.class will be stored in the same subdirectory.
• import package1.ClassA;
class PackageTest1
{
public static void main(String args[ ])
{
ClassA objectA = new ClassA();
objectA.displayA();
}
}
• This source file should be saved as PackageTest1.java and then compiled. The source file and
the compiled file would be saved in the directory of which package1 was a subdirectory.

You might also like