Why use Packages?

Download Report

Transcript Why use Packages?

Packages
• What is a Package?
• Why use Packages?
• Creating a Package
• Naming a Package
• Using Package Members
• Managing Source and Class Files
• Visibility Modifiers
Unit 05
1
What is a Package?
• A package is a collection of related classes and interfaces that provides
access protection and namespace management.
• The classes in Java API are organized into packages. For example:
• Applet classes are in java.applet package
• I/O classes are in java.io package
• GUI classes are in java.awt.
• You can also organize the classes you
create into packages and you should. Why?
Unit 05
2
Why use Packages?
• Easy to know where to find classes and interfaces that provide a particular
set of functions. –For I/O functions go to java.io package.
• Avoid conflict with class names in other packages.
Classes in different packages can have same name.
–A package provides a new namespace.
• You can allow classes within a package to have access to each other.
Unit 05
3
Creating a Package
• To create a package, put a package statement at the top of each source file
for the class or interface that belongs to the package.
• For example, to create a package, graphics, to store the classes: Shape,
Circle, Rectangle, and Square, we should add the statement, package
graphics; as the first statement in each of the source files.
1. package graphics;
2. public abstract class Shape{
3.
public String name(){
4.
return getClass().getName();
5.
}
6.
public abstract double area();
7.
public abstract perimter();
8.
public String toString(){
9.
return name()+"\nPerimeter: "+perimeter()+"\nArea: "+area();
10.
}
11. }
Unit 05
4
Creating a Package-Cont’d
• The scope of the package statement is the entire source file,
so if source file contains more than one class or interface,
they all belong to the package specified by the package
statement.
• If package is not specified for a class or interface, then the
class or interface belongs to the default package, which is a
package that has no name.
Unit 05
5
Naming a Package
• By convention, packages are named using lower case letters.
• Dots are used to group related packages together.
• For example, the packages: java.awt, java.io, java.lang, are named as such
to indicate that they all belong to Java API.
• Similarly, java.awt.color, java.awt.geom, java.awt.event, are named as such
to indicate that they all belong to a group of classes for programming GUI
applications.
• Notice that this grouping is for the human reader only. As far as the system
is concerned, the packages java.awt.color is completely independent of
java.awt.geom.
Unit 05
6
Naming a Package-Cont’d
• Within one organization, it is clear that packages can be used to avoid
conflict in class names. What about between different organizations?
• It is likely that two organizations working on similar projects may come up
with the same package names and probably common class names. Thus, a
user dealing with these two organizations may face conflict.
• To avoid this, by convention, organizations use their reversed Internet
domain name in their package names, like: com.company.package
• We shall name our packages in this course starting with the prefix, ics201.
Example: ics201.graphics, ics201.lab01, etc.
Unit 05
7
Using Package Members
• To use a public package member (a class or
interface) from outside its package, you must
do one of the following:
o Refer to the member by its long name
o Import the package member, or
o Import the member's entire package.
• Each of these methods is appropriate for
different situations, as explained in the
following sections.
Unit 05
8
Referring to a Package Member by Full Name
• So far, our examples have referred to classes and interfaces by their simple
names, such as Rectangle.
• You can use a package member’s simple name if:
– The code you are writing is in the same package as the member
– If the member’s package has been imported.
• However, if a member is from a different package and that package has not
been imported, you must use the member’s fully qualified name:
ics201.graphics.Rectangle
• To create an instance of the member we use:
ics201.graphics.Rectangle myRect = new ics201.graphics.Rectangle();
Unit 05
9
Importing a Package Member
• To import a specific package member into the current file, put an import
statement at the beginning of the file before any class or interface definitions
but after the package statement ( if there is one).
1.
2.
3.
4.
5.
package ics201.lab02;
import ics201.graphics.Shape;
import ics201.graphics.Rectangle;
import ics201.graphics.Square;
import ics201.graphics.Circle;
6. public class TestShapes{
7.
public static void main(String[] args){
8.
Shape[] shapes = new Shape[2];
9.
shape[0] = new Rectangle(5, 10);
10.
shape[0] = new Square(10);
11.
12.
for(int i = 0; i<shape.length; i++)
13.
System.out.println(shape[i]);
14.
}
15. }
•
This approach is used if few members are needed from a package.
Unit 05
10
Importing an Entire Package
•
To import all of the classes and interfaces contained in a particular package, use
the asterisk (*).
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
package ics201.lab02;
import ics201.graphics.*;
public class TestShapes{
public static void main(String[] args){
Shape[] shape = new Shape[2];
shape[0] = new Rectangle(5, 10);
shape[0] = new Square(10);
•
As noted earlier, ics201 and ics201.graphics are independent packages, so the
statement, import ics201.*; only imports ics201 package but not ics201.graphics.
for(int i=0; i< shape.length; i++)
System.out.println(shape[i]);
}
}
Unit 05
11
Importing an Entire Package- Cont’d
• Also note that the asterisk cannot be used to match a subset of a package.
For example, the following does not match all the classes in the graphics
package that begin with a:
• For convenience, the Java runtime system automatically imports three
entire packages:
o The default package (the package with no name)
o The java.lang package
o
The current package
• What if two packages have classes with the same name and both packages
are imported? -- You must use fully qualified names.
Unit 05
12
Managing Source and Class Files
• Declaring package names at the beginning of source files is what is
required to create a package.
• However, for the Java system to locate your source and class files, you
must store them in a directory whose name matches the name of their
package.
• For example, the source code for the Rectangle class should be in a file,
Rectangle.java, and must be in a directory, ics201\graphics. The
ics201\graphics directory can be anywhere on the file system.
Unit 05
13
Managing Source and Class Files- Cont’d
• Note however, that the .class and .java files do not have to be in the same
directory as shown below:
• Since the directory structure can be anywhere in the file system, it is obvious
that the package name alone does not provide a java tool, such as the
Compiler, with all the information need to locate a class.
• The remaining information is provided by CLASSPATH.
Unit 05
14
Managing Source and Class Files- Cont’d
• A class path is an ordered list of directories or jar files in which to search
for class files.
• Each directory listed in the class path is a top-level directory in which
package directories appear.
• For example, suppose that both .java and .class files for the ics201.graphics
package are stored in the same directory. Suppose also that the directory
structure for the package is in a directory z:\cs, as shown below, then we
must add z:\cs to the class path:
Unit 05
15
Visibility Modifiers
• Now that we have learnt about packages, this is a good time to summarize
what we learnt about visibility modifiers.
• Visibility modifiers are used to allow/deny access to other classes or
interfaces.
• There are four of them, namely, public, private, and protected. The fourth,
the default, does not have a keyword, but is assumed if none of the other
three is specified.
Unit 05
16
Example
1. package package1;
2. public class A{
3.
public A(){
4.
System.out.println("Inside Class A");
5.
}
6.
int method1(int x){
7.
return x*x;
8.
}
9.
public int method2(int x){
10.
return method1(x) + method1(x);
11. }
12. }
Unit 05
17
Example (cont’d)
1.
package package2;
2.
import package1.*;
3.
public class B extends A{
4.
public B ( ){ System.out.println ("Inside Class B"); }
5.
public int method1 ( int x ) {
6.
A a = new B();
7.
return a.method2 (x);
8.
}
9.
public int method2 ( int x ){return x; }
10.
public static void main ( String [] x ) {
11.
B b = new B ( );
12.
System.out.println ( b.method1 ( 3 ) );
13.
}
14. }
Unit 05
18
Exercises
1. Add the statement: package ics202.graphics; to each of our shape classes,
except TestShape. Store all the files in some root folder. Try compiling and
running the TestShape class. What happens?
2. Create a folder, ics201\graphics, and store the shape files in it, except the
TestShape class. Do not put any import statement in the TestShape. Try
compiling and running TestShape. What happens?
3. Add the import statement, import ics201.graphics.*; to TestShape Try
compiling and running TestShape without giving the class path for the
ics201.graphics package. What happens?
4. Try compiling and running TestShape, giving the class path for the
ics201.graphics package. It should compile and run successfully.
5. Try each of the methods of importing classes discussed in this session and
the TestShape class.
6. Add the import statement; java.awt.*; What happens? How can this
problem be resolved?
Unit 05
19