Download presentation
Presentation is loading. Please wait.
Published byKimberly Malone Modified over 8 years ago
1
Java Packages What are Java packages? Why do we need packages? How to create a Java package? Some examples. Coming Next: Program Documentation.
2
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: A Java package can have members of either or both of the following kinds: Subpackages of the package. Types declared in the compilation units.
3
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 package java.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.
4
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. suppose you wrote a Date class and there is already another class called Date To control access. A package is like a 'project' in C
5
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: 1.Because the package java.awt has a subpackage image, it cannot (and does not) contain a declaration of a class or interface type named image. 2.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 cannot be any package with the fully qualified name mouse.Button or mouse.Button.Click. 3.If COM.Sun.java.jag is the fully qualified name of a type, then there cannot be any package whose fully qualified name is either COM.Sun.java.jag or COM.Sun.java.jag.scrabble.
6
Java Packages Packages and Subpackages (Cont’d) 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: Methods
7
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.
8
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.
9
Java Packages How to create a Java Package? An Example 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 Point.java 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 Rectangle.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 Square.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 Circle.java public interface Draggable { } // End of interface Draggable Draggable.java
10
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.
11
Java Packages Creating a Java Package 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: 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 Point.java 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 Rectangle.java Package geometry; public interface Draggable { } // End of interface Draggable Draggable.java Java Package Statement
12
Java Packages Creating a Java Package (Cont’d) You include the package statement in all the Java files you intend to include in your package. Java Package Statement 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 Circle.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 Square.java 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.
13
A Practical Example package calculation.twovar; public class DIVISION{ int var1; int var2; public DIVISION(int a,int b) { var1=a; var2=b; } public float getDIVISION() { return var1/var2; }
14
A Practical Example package calculation.twovar; public class PRODUCT{ int var1; int var2; public PRODUCT(int a,int b) { var1=a; var2=b; } public int getPRODUCT() { return var1*var2; }
15
A Practical Example package calculation.twovar; public class SUM { int var1; int var2; public SUM(int a,int b) { var1=a; var2=b; } public int getSUM() { return var1+var2; }
16
A Practical Example package calculation.twovar; public class DIFFERENCE{ int var1; int var2; public DIFFERENCE(int a,int b) { var1=a; var2=b; } public int getDIFFERENCE() { return var1-var2; }
17
A Practical Example import calculation.twovar.*; public class MyClass { public static void main(String[] arg) { int a=10; int b=20; PRODUCT s=new PRODUCT(a,b); System.out.println("The Product of a:"+a+" & "+"b:"+b+"is ="+s.getPRODUCT()); DIFFERENCE d =new DIFFERENCE(b,a); System.out.println("The Difference of b:"+b+" & "+"a:"+a+"is ="+d.getDIFFERENCE()); DIVISION dd =new DIVISION(b,a); System.out.println("The Division of b:"+b+" & "+"a="+a+"is ="+dd.getDIVISION()); SUM ss=new SUM(a,b); System.out.println("The SUM of a:"+a+" & "+"b:"+b+"is ="+ss.getSUM()); }
18
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. e.g. both java.util and java.sql have a Date class, so if you are using both packages you need to reference with java.util.Date or java.sql.Date.
19
Java Packages Java Packages and Host File System 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: C Drive windows java msoffice mypack 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 applet awt io net lang
20
Java Packages Java Packages and Host File System (Cont’d) The subdirectory mypacks may contain your own packages that you have created for your personal use and to share with other Java programmers. mypacks geometry physics science Still continuing the example, if we were to look inside the subdirectory geometry,we might see the following files: Circle.javaCircle.class Draggable.javaDraggable.class Point.javaPoint.class Rectangle.javaRectangle.class Square.javaSquare.class
21
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.
22
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.
23
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.
24
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.
25
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;
26
Java Packages Automatically Imported Packages It is should be noted that the Java runtime system automatically imports three packages for you: 1.The default package (the package with no name). 2.The java.lang package. 3.The current package.
27
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 cs.ics.Shapes; class Point { int x, y; } Thus, the fully qualified name of class Point is: cs.ics.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(“Hello World."); } } defines a very simple compilation unit as part of an unnamed package.
28
Visibility and Packages
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.