2.5.11 Representing Private Fields and Methods in Class Diagrams

Representing Private Fields and Methods in Class Diagrams

A Player can expose a controlled public operation while keeping its stored turn count and helper method private.

Read the Code Before Drawing

This complete class permits a caller to record a turn but not to assign the private count directly.

If the underlying C# feature is unfamiliar, use the lesson below while reading this example.

Source to translate
public class Player
{
    private int numberOfTurns;

    public void RecordTurn()
    {
        IncreaseTurnCount();
    }

    private void IncreaseTurnCount()
    {
        numberOfTurns = numberOfTurns + 1;
    }
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define a Player with a private turn-count field
Offer a public RecordTurn operation to callers
Inside it, call a private IncreaseTurnCount helper
The helper changes the stored count
Describe the visibility of each member separately

These words describe the class contract, not a sequence of runtime actions. Identify the types, stored members, operation inputs and results, and relationships before choosing compartments and connectors.

Map the Source to Class Boxes and Connectors
Build the Diagram in Violet
  1. Create a class diagram with one Player box.
  2. In the attributes compartment enter -numberOfTurns : int. Preserve the minus marker for private.
  3. In operations enter +RecordTurn() : void and -IncreaseTurnCount() : void on separate lines.
  4. Keep all three members visible in this design view. Do not underline them or place their execution order into the class box.
  5. Save and read the two operation contracts: external code can call RecordTurn, while the private helper is used by the class implementation.
Compare the Completed Soccer Diagram

Compare this reference with the diagram you built in Violet. Layout may differ; the source meaning, labels, and relevant connections should agree.

Player keeps numberOfTurns and IncreaseTurnCount private while exposing public RecordTurn.
Public entry point, private storage and helper
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

External code can call RecordTurn. The Player implementation can call its own private helper, which updates the private field.

Verify the Drawing Against the Source
Predict and Check a Small Change