Classes in UML
Classes in UML
Every software system you will ever model is made up of things that have characteristics and can do something. In UML, those things are captured as classes. Before you draw a single line on a class diagram, you need to know exactly how a class is drawn and what each part means. That is what this lesson is about.
The Three-Compartment Class Box
A UML class is drawn as a rectangle divided into three horizontal compartments:
- Name compartment (top) — the class name, centered, written in PascalCase (e.g.,
BookCopy,Appointment). - Attributes compartment (middle) — the data the class holds (its properties or fields).
- Operations compartment (bottom) — the behaviors the class can perform (its methods or functions).
Visibility Markers
Every attribute and operation begins with a visibility marker — a single symbol that declares who can access that member. There are four markers defined by the UML standard:
+public — accessible from anywhere-private — accessible only within this class#protected — accessible within this class and its subclasses~package — accessible within the same package or namespace
Attribute Syntax
The full syntax for an attribute in UML is:
In practice you will write something like:
The : Type and = defaultValue parts are optional but valuable — they remove ambiguity when handing off to developers.
Operation Syntax
The full syntax for an operation is:
Examples from a clinic booking system:
A Real Example: The Appointment Class
Consider a private clinic that books patient appointments. After gathering requirements, the analyst identifies an Appointment class. Here is how it would be drawn:
Appointment, not appointments). Attribute names are camelCase starting with a lowercase letter (appointmentDate). Operation names are camelCase verbs (confirm, cancel). Consistent naming makes diagrams readable to every team member.
What to Put in Attributes vs. Operations
A common early mistake is blurring the line between data and behavior. Use this rule of thumb:
- Attribute — a fact or characteristic that describes the object. It is a noun or noun phrase:
price,emailAddress,quantity. - Operation — something the object can do or compute. It is a verb or verb phrase:
calculateTotal(),sendConfirmation(),isExpired().
For an online store, a Product class holds price and stockLevel as attributes, but applyDiscount(rate : Decimal) and checkAvailability(quantity : Integer) as operations. The data lives in attributes; logic lives in operations.
Suppressing Compartments
In a large diagram with many classes, showing every attribute and operation makes the drawing unreadable. UML allows you to suppress (hide) one or both lower compartments. An empty middle or bottom compartment signals deliberate suppression, not an empty class. You will often draw high-level diagrams showing only class names, then add detail only to the classes most relevant to the current discussion.
...) inside the empty compartment to make the intent explicit.
Summary
Every UML class box has three compartments: the name at the top, attributes in the middle, and operations at the bottom. Visibility markers (+, -, #, ~) prefix each member. Following precise attribute and operation syntax — type, default value, parameters, return type — eliminates ambiguity and makes your models directly useful to developers. In the next lesson you will learn how to find these classes by reading and analyzing real requirements documents.