Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 39 ยท Class and Object Diagrams

Classes, Attributes and Operations

Learn how to draw a UML class: the three-compartment box, attributes with types and defaults, operations with parameters, static and abstract members, and good class design.

The class diagram is the most widely used UML diagram. It shows the types in a system and how they relate, and it maps closely onto object-oriented code. Everything in a class diagram starts with the class box, so this lesson covers one class in detail: its name, its attributes, its operations and the small marks that describe them.

The three compartments

A class is a rectangle divided into up to three compartments:

  1. Name (always shown).
  2. Attributes (the data the class holds).
  3. Operations (what the class can do).
classDiagram
    class Book {
        -String isbn
        -String title
        -int copiesAvailable
        +isAvailable() bool
        +checkOut()
        +returnCopy()
    }

A Book with three attributes and three operations. Hide the compartments you do not need.

You may omit a compartment when it adds nothing. A diagram about relationships often shows only class names.

Attributes

The full attribute syntax is:

TEXT
visibility name : type [multiplicity] = default {property}

Examples: -title : String, -tags : String [0..*], +MAX_LOAN_DAYS : int = 14 {readOnly}.

  • The type may be a primitive, another class or a collection.
  • A default value follows =.
  • Derived attributes start with a slash: /age is computed from birthDate.
  • A static (class-level) attribute is underlined in UML. In Mermaid, append $: +int count$.

Mermaid uses the type-first style that many languages share (String title), while the UML specification writes title : String. Both are understood; pick one per diagram and stay consistent.

Operations

TEXT
visibility name(direction param : type, ...) : returnType

Examples: +deposit(amount : float), +findByIsbn(isbn : String) : Book, #validate() : bool.

  • A parameter direction (in, out, inout) is optional and rarely needed.
  • Abstract operations are written in italics (Mermaid appends *: +draw()*).
  • A class with abstract operations is abstract and its name is italic as well.
  • Constructors are conventionally omitted unless they matter.
  • Getters and setters are noise. Leave them out unless the design depends on them.
classDiagram
    class Shape {
        <<abstract>>
        #String color
        +area() double*
        +describe() String
    }
    class IdGenerator {
        -int next$
        +nextId() int$
    }

An abstract Shape with an abstract operation, and a utility class with static members.

Responsibility: what a class is for

A good class has one clear reason to exist, which you can state in a sentence. If your sentence needs "and" twice, split the class. Name the responsibility, then check that attributes and operations serve it.

Class Responsibility Smells
Invoice Holds billing data for one order has sendEmail() (that is a notification job)
Cart Collects items before checkout has chargeCard() (that is a payment job)
Temperature Holds one reading with a unit fine: small and focused
Python Describe a class as data and draw it as text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

From box to code

A class box translates almost line for line into code. The attributes become fields, the operations become methods, and visibility becomes access modifiers.

Python The Book class as Python code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Levels of detail

The same class appears at different levels depending on the purpose.

One class, three levels

Name only Book. For overviews.
Name + key members Design discussions.
Full signatures Types, defaults, constraints.

Show only what the reader needs.

1 / 2
  • Conceptual level (analysis): names and meaningful attributes, no types, nothing about code.
  • Specification level (design): operations and types agreed between developers.
  • Implementation level: language-specific details; usually the code itself is the better document.

When you are exploring a domain, draw classes with names and a few attributes only. Adding types and visibility too early freezes decisions you have not made yet.

Common mistakes

  • Data bags with no behavior. A class with only getters and setters often signals that behavior lives somewhere it should not.
  • God classes. One box with thirty operations usually hides three classes.
  • Attributes that should be relationships. If Order has an attribute customer : Customer, draw an association line instead (next lesson).
  • Implementation noise. Skipping id fields, toString() and trivial accessors keeps the diagram readable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three compartments of a UML class box, top to bottom?

    The name comes first, then attributes, then operations.
  2. 2. How is an abstract operation shown?

    Abstract classes and operations are italic; static members are underlined.
  3. 3. What does a slash before an attribute name (/age) mean?

    A derived attribute is calculated, for example age from birthDate.
  4. 4. Which is the best sign that a class should be split?

    A class should have one clear responsibility.

Summary

  • A class box has name, attribute and operation compartments; hide those you do not need.
  • Attributes: visibility name : type = default; operations: visibility name(params) : return.
  • Static members are underlined, abstract ones italic; /x marks a derived attribute.
  • Give every class one stated responsibility.
  • Choose the level of detail (conceptual, specification, implementation) by purpose.

Next: Associations and Multiplicity