How to Diagram Java Classes with UML
Since the very beginning of computer programming, before the popularity of Java, programmers have loved to create diagrams of their programs. Originally, they drew flowcharts that graphically represented a program’s procedural logic.
Flowcharts were good at diagramming procedures, but they were way too detailed. When the structured programming craze hit in the 1970s, and programmers started thinking about the overall structure of their programs, they switched from flowcharts to structure charts, which illustrated the organizational relationships among the modules of a program or system.
Now that object-oriented programming is the thing, programmers draw class diagrams to illustrate the relationships among the classes that make up an application. This image shows a class diagram of a simple system that has four classes. The rectangles represent the classes themselves, and the arrows represent the relationships among the classes.
You can draw class diagrams in many ways. To add some consistency to their diagrams, most programmers use a standard called UML, which stands for Unified Modeling Language. The class diagram in above is an example of a simple UML diagram, but UML diagrams can get much more complicated.
The following information describe the details of creating UML class diagrams. Note that it doesn’t even come close to explaining all the features of UML.
The basic element in a class diagram is a class. In UML, each class is drawn as a rectangle. At minimum, the rectangle must include the class name. You can subdivide the rectangle into two or three compartments that can contain additional information about the class.
The middle compartment of a class lists the class variables, whereas the bottom compartment lists the class methods. The name of each variable or method can be preceded by a visibility indicator, which can be one of the symbols listed below. (In actual practice, it’s common to omit the visibility indicator and list only those fields or methods that have public visibility.)
If you want, you can include type information for variables as well as for methods and parameters. The type of a variable is indicated by following the variable name with a colon and the type:
A method’s return type is indicated in the same way:
Parameters are listed within the parentheses, and both the name and type are listed. For example:
getCustomer(custno: int): Customer
Note: Omitting the type and parameter information from UML diagrams is common.
Interfaces are drawn pretty much the same way as classes, but the class name is preceded by the word interface:
Note: The word interface is enclosed within a set of double-left and double-right arrows. These arrows aren’t just two less-than or greater-than symbols typed in a row; they’re a special combination of symbols. Fortunately, the double-arrow symbol is a standard part of the ASCII character set. You can access it in Microsoft Word via the Insert Symbol command.
Besides using rectangles to represent classes, class diagrams include arrows to represent relationships among classes. UML uses a variety of types of arrows.
- A solid line with a hollow closed arrow at one end represents inheritance.
- The arrow points to the base class. A dashed line with a hollow closed arrow at one end indicates that a class implements an interface.
- The arrow points to the interface. A solid line with an open arrow indicates an association.
An association simply indicates that two classes work together. It may be that one of the classes creates objects of the other class or that one class requires an object of the other class to perform its work. Or perhaps instances of one class contain instances of the other class.
You can add a name to an association arrow to indicate its purpose. If an association arrow indicates that instances of one class create objects of another class, you can place the word
Creates next to the arrow.