Card 01/ 08
UML Class Boxes: Name, Fields, Methods and Visibility
Plus is public, minus is private, hash is protected, and italics mean abstract.
A UML class box is a rectangle with up to three compartments: the class name at the top, its fields in the middle, and its methods at the bottom. UML, the Unified Modeling Language, is a standard notation for drawing software designs, and its class diagram is how every design pattern is usually shown. This card covers every mark you will meet inside one box, so that a box reads as precisely as the class it describes.
Each field and method starts with a symbol for its visibility, which says what other code may use it. A field is written as its name and type, and a method as its name, its parameters and the type it returns.
| Mark | Meaning | In the diagram |
|---|---|---|
+ | Public: any code may use it. | +owner and +deposit |
- | Private: only the class itself. | -balance |
# | Protected: the class and its subclasses. | #history |
~ | Package: code in the same package or module. | ~branch |
| Italics | Abstract: declared here, written by a subclass. | fee() |
| Underline | Static: belongs to the class, not to each object. | count() |
<<interface>> | The box is an interface, a list of methods with no fields or bodies. | Auditable |
<<abstract>> | The class cannot be created directly; a subclass must be. | Account |
The words between << and >> are called stereotypes. They label a box as a special kind of class. Many tools draw an abstract class by writing its name in italics instead, and this subject writes the stereotype, since it reads the same on every screen.
Diagrams leave things out on purpose. A pattern diagram usually shows only the members that take part in the pattern, so a box with two methods does not mean the class has only two. The marks say what the design depends on, and the rest is left to the code.
Read this way, a box answers three questions at a glance: what the class is called, what it keeps, and what other classes may ask of it. Every pattern diagram in this subject is built from boxes like these.
← → move through sections and cards