Presentation is loading. Please wait.

Presentation is loading. Please wait.

Packages Written part of final exam Alex Rudniy

Similar presentations


Presentation on theme: "Packages Written part of final exam Alex Rudniy"— Presentation transcript:

1 Packages Written part of final exam Alex Rudniy
CIS 602 Java and the Web Packages Written part of final exam Alex Rudniy

2 Packages To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages. Definition:  A package is a collection of related types providing access protection and name space management. Note that types refers to classes, interfaces, enums, and annotations. 11/16/2018

3 Packages Provide and enforce a contract between the developer and the user of a collection of resources Reuse Organize resources so they can be used in more than one program Organize the name space Avoid naming conflicts Helps to organize the software Group related resources 11/16/2018

4 Packages Organize smaller components into larger components
Helps to organize work effort Divide and Conquer a large body of software Allow multiple programmers to work on same effort Encapsulation Package is a collection of related classes or packages 11/16/2018

5 Graphical Notation Packages are graphically drawn as a rectangle with a tab on the left hand side. Further packages contained within the main package may be drawn in the rectangle. If this is the case, the name of the main package is shown in the tab. Otherwise it is shown in rectangle. 11/16/2018

6 Predefined packages Predefined packages come with Java
Group of all packages known as Java class library or Java API Two groups of packages in Java API: Core packages begin with java import java.util.Vector; Extension or Extra packages begin with javax import javax.swing.JOptionPane; 11/16/2018

7 Core Packages The Java class library is organized into a number of packages: java.applet - classes necessary and beneficial for creating classes to go on the web java.awt - classes for defining GUIs java.awt.datatransfer - for cut and paste java.awt.event - for handling button presses, mouse moves, etc java.awt.image - for handling images 11/16/2018

8 Core Packages java.io - handling input/output
java.lang - classes for primitive types other essential classes java.math - math functions java.net - networking functions java.text - text handling functions java.util - classes not well defined in other classes but very helpful java.util.zip - package for handling, creating and using zip files, compression 11/16/2018

9 Extension Packages javax.crypto - for cryptographic operations.
javax.net - for networking applications. javax.print - describe the types of JavaTM Print Service attributes javax.swing - a set of "lightweight" components that, to the maximum degree possible, work the same on all platforms. javax.sound.sampled - for capture, processing, and playback of sampled audio data. javax.security.cert - public key certificates. 11/16/2018

10 Created packages Software developers organize own classes into packages Naming conventions packages are all lower case commonly use reverse of internet domain name to provide a globally unique name i.e. com.sun.java.swing The first line of the unit declares the package to which the class belongs: package mypackage.subpackage.subsubpackage; 11/16/2018

11 Named and unnamed For example, it is possible to declare a package:
package vector; public class Vector {Object[] vec;} 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(“Hi Mr. Student”); } } defines a very simple compilation unit as part of an unnamed package. 11/16/2018

12 Import Packages If you have classes in packages, you can then reference them in different ways : with the fully qualified domain name packagename.classname, for example, geometry.Point myPoint = new geometry.Point(); or use an import statement import packagename.classname; //one class import packagename.*; //entire package Elephant myElephant = new Elephant(); 11/16/2018

13 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 each member by its qualified name. For example, a developer defined a class named Rectangle in the graphics package. The java.awt package also contains a Rectangle class. 11/16/2018

14 Disambiguating a Name If both graphics and java.awt have been imported, the following is ambiguous. Rectangle rect; In such a situation, you have to be more specific and use the member's qualified name to indicate exactly which Rectangle class you want. graphics.Rectangle rect; 11/16/2018

15 Directory structure If you have classes in packages, the directories on the file system they reside in must match the package structure. For example, I have a directory called d:\java Then my directory structure would have d:\java\zoo\Elephant.java after compiling would give us d:\java\zoo\Elephant.class 11/16/2018

16 Tips Only public classes or interfaces are accessible outside the package in which they are defined. Packages may be uncompressed (i.e. directories) or compressed (i.e. zip files) Package directories may be either placed in the current directory, or on the CLASSPATH. The Java VM automatically imports java.lang and the current package you are using 11/16/2018

17 Compile and Run javac packagenames.classname
To compile you should to move out to the top source directory (above your packages) and type javac packagenames.classname d:\java> javac zoo\Elephant.java to run the program, you use the dot notation instead d:\java> java zoo.Elephant 11/16/2018

18 Summary To create a package, put a type (class, interface, enum, or annotation) in it. To put a class or an interface into a package, put a package statement as the first statement in the source file for the class or the interface. The path name of the source and class file of a class or an interface mirrors the name of the package. 11/16/2018

19 Summary To use a class or an interface that's in a different package, you have three choices: use the fully qualified name of the class or the interface import the class or the interface import the entire package of which the class or the interface is a member. 11/16/2018

20 Summary You might have to set your class path so that the compiler and the interpreter can find the source and class files for your classes and interfaces. 11/16/2018

21 Bibliography Jia, Xiaoping, Object Oriented Software Development Using Java. Addison Wesley, 2003 11/16/2018


Download ppt "Packages Written part of final exam Alex Rudniy"

Similar presentations


Ads by Google