250 likes | 574 Views
Java Packages. What are Java packages? Why do wee need packages? How to create a Java package? Some examples. Coming Next: Program Documentation. What are Java Packages?.
E N D
Java Packages • What are Java packages? • Why do wee need packages? • How to create a Java package? • Some examples. • Coming Next: Program Documentation. Java Packages
What are Java Packages? • Definition 1: A package is a collection of related classes and interfaces that provides access protection and namespace management. • Definition 2: Java programs are organized as sets of packages. Each package has its own set of names for types, which helps to prevent name conflicts. A type is accessible outside the package that declares it only if the type is declared public. The naming structure for packages is hierarchical. The members of a package are class and interface types, which are declared in compilation units of the package, and subpackages, which may contain compilation units and subpackages of their own. • Package Members: AJava package can have members of either or both of the following kinds: • Subpackages of the package. • Types declared in the compilation units. Java Packages
What are Java Packages? (Cont’d) • For example, in the standard Java Application Programming Interface (API): • The package java has subpackages: applet, io, lang, net, awt, and util, but no compilation units. • The packagejava.awt has a subpackage named image, as well as a number of compilation units containing declarations of class and interface types. • If the fully qualified name of a package is P, and Q is a subpackage of P, then P.Q is the fully qualified name of the subpackage. • For the subpackages of package java named lang, util, and io,the fully qualified package names are therefore java.lang, java.util, and java.io. Java Packages
What do we need Java Packages? • Java programmers bundle groups of related classes into packages: • To make classes easier to find and use. • To avoid naming conflicts. • To control access. Java Packages
Packages and Subpackages • A package may not contain a type declaration and a subpackage of • the same name, or a compile-time error results. Here are some examples: • Because the package java.awt has a subpackage image, it cannot (and does not) contain a declaration of a class or interface type namedimage. • If there is a package named mouse and a type Button in that package(which then might be referred to as mouse.Button), then there cannotbe any package with the fully qualified name mouse.Button or mouse.Button.Click. • If COM.Sun.java.jag is the fully qualified name of a type, then therecannot be any package whose fully qualified name is either COM.Sun.java.jag or COM.Sun.java.jag.scrabble. Java Packages
Packages and Subpackages (Cont’d) Methods • The java package includes several other sub-packages, such as java.io, • which provides facilities for input/output, java.net, which deals with • network communication, and java.applet, which implements the basic • functionality of applets. The most basic package is called java.lang, • which includes fundamental classes such as String and Math. • It might be helpful to look at a graphical representation of the levels of • nesting in the java package, its sub-packages, the classes in those • subpackages, and the methods in those classes. This is not a complete • picture, since it shows only a few of the many items in each element: Java Packages
Compilation Units • A compilation unit consists of three parts, each of which is optional: • A package declaration, giving the fully qualified name of the • package to which the compilation unit belongs. • import declarations that allow types from other packages to be • referred to using their simple names. • Type declarations of class and interface types. Java Packages
How to create a Java Package? As mentioned earlier, the classes and interfaces that are part of the JDK are members of various packages that bundle classes by function: applet classes are in java.applet, I/O classes are in java.io, and the GUI widget classes are in java.awt. You can put your own classes and interfaces in packages, too. Let's look at a set of classes and examine why you might want to put them in a package. Suppose you write a group of classes that represent a collection of graphics objects such as circles, rectangles, lines, and points. You also write an interface Draggable that classes implement if they can be dragged with the mouse by the user. Java Packages
How to create a Java Package? An Example Point.java Rectangle.java public class Point { int x_coord; int y_coord; public Point() { x_coord = 0; y_coord = 0; } // End of Point Constructor 1 public Point(int x, int y) { x_coord = x; y_coord = y; } // End of Point Constructor 2 } // End of class Point public class Rectangle extends Point { double width; double height; public Rectangle(int x, int y, double h, double w ) { super(x, y); width = w; height = h; } // End of Rectangle Constructor } // End of class Rectangle Square.java public class Square extends Point { double edge; public Square(int x, int y, double e) { super(x, y); edge = e; } // End of Square Constructor } // End of class Square Circle.java public class Circle extends Point { double radius; public Circle(int x, int y, double r) { super(x, y); radius = r; } // End of Circle Constructor } // End of class Circle Draggable.java public interface Draggable { } // End of interface Draggable Java Packages
How to create a Java Package? An Example (Cont’d) • Now, we would like to to put these classes and the interface together in a package, for several reasons: • You and other programmers can easily determine that these classes and interfaces are related. • You and other programmers know where to find classes and interfaces that provide graphics-related functions. • The names of your classes won't conflict with class names in other packages because the package creates a new namespace. • You can allow classes within the package to have unrestricted access to each other, yet still restrict access for classes outside the package. Java Packages
Creating a Java Package Point.java Rectangle.java package geometry; public class Point { int x_coord; int y_coord; public Point() { x_coord = 0; y_coord = 0; } // End of Point Constructor 1 public Point(int x, int y) { x_coord = x; y_coord = y; } // End of Point Constructor 2 } // End of class Point package geometry; public class Rectangle extends Point { double width; double height; public Rectangle(int x, int y, double h, double w ) { super(x, y); width = w; height = h; } // End of Rectangle Constructor } // End of class Rectangle Draggable.java Package geometry; public interface Draggable { } // End of interface Draggable Now, you can easily create your own package, say geometry, and put all the above Java class and interface definitions in it. Simply, add the package statement at the to of all the previous Java files as follows: Java Package Statement Java Packages
Creating a Java Package (Cont’d) Square.java package geometry; public class Square extends Point { double edge; public Square(int x, int y, double e) { super(x, y); edge = e; } // End of Square Constructor } // End of class Square Circle.java package geometry; public class Circle extends Point { double radius; public Circle(int x, int y, double r) { super(x, y); radius = r; } // End of Circle Constructor } // End of class Circle You include the package statement in all the Java files you intend to include in your package. Java Package Statement If you do not use a package statement, your class or interface ends up in the default package, which is a package that has no name. Generally speaking, the default package is only for small or temporary applications or when you are just beginning development. Otherwise, it is recommended to have classes and interfaces belonging to named packages. Java Packages
Package Naming With programmers all over the world writing Java classes and interfaces, it is conceivable and even likely that two programmers will use the same name for two different classes. In fact, the previous example does just that: It defines a Rectangle class when there is already a Rectangle class in the java.awt package. Yet, the compiler allows both classes to have the same name because they are in different packages and the actual name of each class includes the package name. That is, the name of the Rectangle class in the geometry package is really geometry.Rectangle, and the name of the Rectangle class in the java.awt package is really java.awt.Rectangle. This works just fine unless two independent programmers use the same name for their packages. In this case, what prevents to programmers to have the same name for their own packages? By simply following the Java convention (to be presented in the Lab). Java Packages
Java Packages and Host File System C Drive windows mypack msoffice java java applet net io lang awt As an extremely simple example, all the packages and source and binary code on a system might be stored in a single directory and its subdirectories. Each immediate subdirectory of this directory would represent a top level package, that is, one whose fully qualified name consists of a single simple name. The directory might contain the following immediate subdirectories: Continuing the example, the directory java would contain, among others, the subdirectories shown below. These subdirectories correspond to the packages java.applet, java.awt, java.io, java.lang, and java.net that are defined as part of the Java API. Java Packages
Java Packages and Host File System (Cont’d) mypacks geometry science physics The subdirectory mypacks may contain your own packages that you have created for your personal use and to share with other Java programmers. Still continuing the example, if we were to look inside the subdirectory geometry,we might see the following files: Circle.java Circle.classDraggable.java Draggable.classPoint.java Point.classRectangle.java Rectangle.classSquare.java Square.class Java Packages
Using Package Members • Only public package members are accessible outside the package in which they are defined. To use a public package member from outside its package, you must either: • Refer to the member by its long (disambiguated) name. • Import the package member. • Import the member's entire package. Java Packages
Referring to Package Members by Names You can use a package member's short name if the code you are writing is in the same package as that member or if the member's package has been imported. However, if you are trying to use a member from a different package and that package has not been imported, then you must use the fully qualified name of the member, which includes the package name. This is the fully qualified name for the Rectangle class declared in the geometry package in the previous slides: geometry.Rectangle You could use this long name to create an instance of geometry.Rectangle: geometry.Rectangle myRect = new geometry.Rectangle(0, 0, 20, 12); You'll find that using long names is okay for one-shot uses. But you'd likely get annoyed if you had to write graphics.Rectangle again and again. Also, your code would get very messy and difficult to read. In such cases, you can just import the member instead. Java Packages
Importing a Package Member To import a specific member into the current file, put an import statement at the beginning of your file before any class or interface definitions (but after the package statement, if there is one). Here's how you would import the Circle class from the geometry package created in the previous slides: import geometry.Circle; Now you can refer to the Circle class by its short name: Circle myCircle = new Circle(0, 0, 15); This approach works just fine if you use just a few members from the geometry package. But if you use many classes and interfaces from a package, you really just want to import the whole package and forget about it. Java Packages
Importing an Entire Package To import all of the classes and interfaces contained in a particular package, use the import statement with the asterisk * wildcard character: import geometry.*; Now you can refer to any class or interface in the geometry package by its short name: Circle myCircle = new Circle(0, 0, 15); Rectangle myRect = new Rectangle(0, 0, 20, 12); The asterisk in the import statement can be used only to specify all of the classes within a package, as shown here. It cannot be used to match a subset of the classes in a package. For example, the following does not match all of the classes in the geometry package that begin with "A": import geometry.A*; // does not work Instead, it generates a compiler error. With the import statement, you can import only single package member or an entire package. Java Packages
Disambiguating a Name If by some chance a member in one package shares the same name with a member in another package and both packages are imported, you must refer to the member by its long name. For example, the previous example defined a class named Rectangle in the geometry package. The java.awt package also contains a Rectangle class. If both geometry and java.awt have been imported, then the following is ambiguous: Rectangle rect; In such a situation, you have to be more specific and indicate exactly which Rectangle class you want by using the member's long name: geometry.Rectangle rect; or: java.awt.Rectangle rect; Java Packages
Automatically Imported Packages • It is should be noted that the Java runtime system automatically imports three packages for you: • The default package (the package with no name). • The java.lang package. • The current package. Java Packages
Named and Unnamed Packages • Named Packages: A package declaration in a compilation unit specifies • the name of the package to which the compilation unit belongs. Here is • an example of named package declaration: • package ics.ics201.Shapes; • class Point { int x, y; } • Thus, the fully qualified name of class Point is: • ics.ics201.Shapes.Point. • Unnamed Packages: A compilation unit that has no package • declaration is part of an unnamed package. As an example, the • compilation unit: • class FirstCall { • public static void main(String[] args) { • System.out.println("Mr. Watson, come here. I want you."); } • } • defines a very simple compilation unit as part of an unnamed package. Java Packages