Download User Defined Packages…

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Packages
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY
FACULTY OF SCIENCE & TECHNOLOGY
UNIVERSITY OF UWA WELLASSA
Introduction
Definition: A package is a grouping of related classes and
interfaces providing access protection and name space
management.
One of the main features of Object Oriented Programming
is its ability to reuse the code that is already created. If it
is necessary to use the classes from other programs
without physically copying them into a new program,
packages can be used.
In Java packages are the way of grouping a variety of
classes and/or interfaces together. The grouping is usually
done according to the functionality of the class. In fact,
2
packages act as “containers” for classes.
By organizing the classes into packages the following
benefits can be achieved.
1. The classes contained in the packages of other
programs can be easily reused.
2. 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. They may
be referred by their fully qualified name (package
name and class name).
3. Packages provide a way to hide classes thus
preventing other programs or packages from accessing
classes that are meant for 3internal use only
Classification of packages
In Java packages are classified into two types,
1. Java API packages.
2. user defined packages.
Java API packages.
Java API (Application Programming Interface) provides a
large number of classes grouped into different packages
according to functionality. Following figure shows some of
the frequently used packages
4
API packages
Using System Packages.
The classes in the Java API are organized in to a hierarchical
structure. Two ways to access the classes stored in a
package.
The first approach is to use the fully qualified class name
of the class that need to be used. This is done by using the
package name containing the class and then appending the
class name to it using the dot operator.
example
Vector in the util package,
java.util.Vector v = new java.util.Vector();
note that the util is a package within the package java and
the hierarchy is represented by separating the levels with
5
dots.
API packages…
In many situations it is necessary to use a class in a number
of places in the program or may need to use many of the
classes contained in the same package.
This can be easily done by using the import statement.
import packagename.classname;
or
import packagename.*; (* represents all the classes of the
package)
These are known as import statements and must appear at
the top of the file, before any class declaration.
6
API packages…
Example
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.
The following statement imports all the classes contained
in the java.awt package.
import java.awt.*;
7
API packages…
Naming conventions
Packages can be named using the standard Java naming
rules. By convention, however, 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.
8
User Defined Packages
Creating the package.
Java provides the facility to create user defined packages.
Need to perform the following steps.
1. Declare the package at the beginning of a file using
the statement,
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 classname.java files in the subdirectory
created.
Note that the subdirectory name must match the
9
package name exactly.
User Defined Packages…
Accessing a package
Similar way as accessing the API packages, the import
command is used;
import package1 [.package2] [.package3] .classname;
Or
import packagename.* ;
10
User Defined Packages…
Example:
This is a Java example of using a package for a application
to handle employee details of a company.
Step 1: Code the Java file giving a package name.
package company;
public class Employee
{
private String name;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return this.name;
}
11
User Defined Packages…
Step 2: Save the file with the name “Employee.java” in a folder
“company”
(Eg: D:\ Java \ company)
Step 3: import the Employee class in the company package into the
driver class.
Save the driver class in the directory D: \ Java.
import company.Employee;
class Test
{
public static void main(String args[])
{
Employee e1=new Employee();
e1.setName("Nimal Perera");
System.out.println(e1.getName());
}
12
User Defined Packages…
Note that in this example the driver class is saved in the
directory D: \ Java.
Therefore it is possible to give the relative path into the
Employee class in the import statement as
company.Employee (or company.*).
But if the driver class is stored in a directory which can not
resolve the relative path to the company package it will
produce a compile time error.
13
User Defined Packages…
To solve this problem, need to set the absolute path to the
place where the package is stored. Then from that place
the relative path company.Employee will be considered.
This is done by setting the classpath environmental
variable.
In the command prompt type;
set classpath = .;D:\Java
Note that the symbol “.” before the absolute path (D:\Java)
is to represent the current working directory of Java. When
this part is omitted the interpreter may not be able to find
the class file of the driver class
14