1.3.8 Methods in UML Class Diagrams

Class Diagrams Can Show Behavior as Well as Fields

You have already modeled fields:

Plain text
Team
-------------------------
name : string
score : int

A UML class diagram can also list methods.

For example:

Plain text
Team
-------------------------
name : string
score : int
-------------------------
DisplayName() : void
ResetScore() : void

The method compartment describes behavior available on the class.

Read a Method Entry

For:

Plain text
DisplayName() : void

read:

Team defines a method named DisplayName that returns no value.

The empty parentheses match the current parameterless-method scope.

Connect UML to C#

UML:

Plain text
ResetScore() : void

C#:

C#
public void ResetScore()
{
    this.score = 0;
}

The UML describes the class interface at a structural level.

The C# supplies the executable statements.

Do Not Add Future Method Detail

Parameters and non-void return values are taught later.

For current class diagrams, use simple parameterless void methods when that matches the source.

Class Diagram and Sequence Diagram Have Different Method Views

A class diagram answers:

Which methods does this type define?

A sequence diagram answers:

Which object calls which method, and in what order?

The same method name can appear in both diagrams for different reasons.