UML: Class & Object Diagrams

Classes in UML

18 min Lesson 1 of 10

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:

  1. Name compartment (top) — the class name, centered, written in PascalCase (e.g., BookCopy, Appointment).
  2. Attributes compartment (middle) — the data the class holds (its properties or fields).
  3. Operations compartment (bottom) — the behaviors the class can perform (its methods or functions).
Three-compartment UML class box structure ClassName attributes + attributeName : Type - anotherAttr : Type = default # protectedAttr : Type operations + operationName() : ReturnType - privateOp(param : Type) : void ① Name ② Attributes ③ Operations
The standard UML three-compartment class box: name (top), attributes (middle), operations (bottom).

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
Analyst tip: You do not need to specify visibility for every attribute at analysis time. During the early stages of systems analysis, it is perfectly acceptable to omit visibility and focus on identifying the right attributes and operations. Add visibility markers when moving toward design and implementation.

Attribute Syntax

The full syntax for an attribute in UML is:

visibility name : Type [ multiplicity ] = defaultValue { constraint }

In practice you will write something like:

- patientName : String - dateOfBirth : Date + isActive : Boolean = true - balance : Decimal { balance >= 0 }

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:

visibility name ( paramName : Type, ... ) : ReturnType

Examples from a clinic booking system:

+ scheduleAppointment(doctorId : Integer, date : Date) : Appointment + cancelAppointment(appointmentId : Integer) : Boolean - validateSlot(date : Date, time : Time) : Boolean

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 class from a clinic booking system Appointment - appointmentId : Integer - appointmentDate : Date - startTime : Time - durationMinutes : Integer - status : String = "Scheduled" - notes : String + confirm() : void + cancel(reason : String) : Boolean + reschedule(newDate : Date, newTime : Time) : Boolean private: enforced internally public: called by other classes
The Appointment class from a clinic booking system — attributes are private, operations are public.
Naming conventions matter. Class names are singular nouns in PascalCase (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.

Do not confuse suppression with omission. If a compartment is omitted entirely (the box has only one or two sections), make sure your audience knows that suppression was deliberate. A common practice is to add an ellipsis (...) 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.