Key Abstractions
In Java, the applet for printing "Hello, World!" in a Web browser is quite simple:
import java.awt.Graphics;
class HelloWorld extends java.applet.Applet {
public void paint (Graphics g) {
g.drawString("Hello, World!", 10, 10);
}
}
The first line of code:
import java.awt.Graphics;
makes the class Graphics directly available to the code that follows. The java.awt prefix specifies the Java package in which the class Graphics lives.The second line of code:
class HelloWorld extends java.applet.Applet {
introduces a new class named HelloWorld and specifies that it is a kind of class just like Applet, which lives in the package java.applet.The next three lines of code:
public void paint (Graphics g) {
g.drawString("Hello, World!", 10, 10);
}
declare an operation named paint, whose implementation invokes another operation, named drawString, responsible for printing the string "Hello, World!" at the given coordinates. In the usual object-oriented fashion, drawString is an operation on a parameter named g, whose type is the class Graphics.Modeling this application in the UML is straightforward. As Figure 3-1 shows, you can represent the class HelloWorld graphically as a rectangular icon. Its paint operation is shown here as well, with all its formal parameters elided and its implementation specified in the attached note.Figure 3-1. Key Abstractions for HelloWorld
NoteThe UML is not a visual programming language, although, as the figure shows, the UML does allowbut does not requirea tight coupling to a variety of programming languages, such as Java. The UML is designed to allow models to be transformed into code and to allow code to be reengineered back into models. Some things are best written in the syntax of a textual programming language (for example, mathematical expressions), whereas other things are best visualized graphically in the UML (for example, hierarchies of classes).This class diagram captures the basics of the "Hello, World!" application, but it leaves out a number of things. As the preceding code specifies, two other classesApplet and Graphicsare involved in this application and each is used in a different way. The class Applet is used as the parent of HelloWorld, and the class Graphics is used in the signature and implementation of one of its operations, paint. You can represent these classes and their different relationships to the class HelloWorld in a class diagram, as shown in Figure 3-2.Figure 3-2. Immediate Neighbors Surrounding HelloWorld
The Applet and Graphics classes are represented graphically as rectangular icons. No operations are shown for either of them, so their icons are elided. The directed line with the hollow arrowhead from HelloWorld to Applet represents generalization, which in this case means that HelloWorld is a child of Applet. The dashed directed line from HelloWorld to Graphics represents a dependency relationship, which means that HelloWorld uses Graphics.This is not the end of the framework upon which HelloWorld is built. If you study the Java libraries for Applet and Graphics, you will discover that both of these classes are part of a larger hierarchy. Tracing just the classes that Applet extends and implements, you can generate another class diagram, shown in Figure 3-3.Figure 3-3. HelloWorld Inheritance Hierarchy
NoteThis figure is a good example of a diagram generated by reverse engineering an existing system. Reverse engineering is the creation of a model from code.This figure makes it clear that HelloWorld is just a leaf in a larger hierarchy of classes. HelloWorld is a child of Applet; Applet is a child of Panel; Panel is a child of Container; Container is a child of Component; and Component is a child of Object, which is the parent class of every class in Java. This model thus matches the Java libraryeach child extends some parent.The relationship between ImageObserver and Component is a bit different, and the class diagram reflects this difference. In the Java library, ImageObserver is an interface, which, among other things, means that it has no implementation and instead requires that other classes implement it. You can show that class Component implements interface ImageObserver by the solid line from the rectangle (Component) to a provided interface circle (ImageObserver).As these figures show, HelloWorld collaborates directly with only two classes (Applet and Graphics), and these two classes are but a small part of the larger library of predefined Java classes. To manage this large collection, Java organizes its interfaces and classes in a number of different packages. The root package in the Java environment is named, not surprisingly, java. Nested inside this package are several other packages, which contain other packages, interfaces, and classes. Object lives in the package lang, so its full path name is java.lang.Object. Similarly, Panel, Container, and Component live in awt; the class Applet lives in the package applet. The interface ImageObserver lives in the package image, which in turn lives in the package awt, so its full path name is the rather lengthy string java.awt.image.ImageObserver.You can visualize this packaging in a class diagram, shown in Figure 3-4. As this figure shows, packages are represented in the UML as a tabbed folders. Packages may be nested, and the dashed directed lines represent dependencies among these packages. For example, HelloWorld depends on the package java.applet, and java.applet depends on the package java.awt.Figure 3-4. HelloWorld Packaging
