3.4.13 Representing Virtual and Overridden Methods in Class Diagrams

Representing Virtual and Overridden Methods in Class Diagrams

A goalkeeper is a Player with specialized turn behavior. The class diagram must preserve inheritance and the operation that is overridden.

Read the Code Before Drawing

These complete class definitions increment a normal turn count and, for a Goalkeeper, also a save-practice count.

Source to translate
public class Player
{
    public int NumberOfTurns;

    public virtual void RecordTurn()
    {
        NumberOfTurns = NumberOfTurns + 1;
    }
}

public class Goalkeeper : Player
{
    public int SavesPracticed;

    public override void RecordTurn()
    {
        base.RecordTurn();
        SavesPracticed = SavesPracticed + 1;
    }
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define Player with a stored turn count and a virtual RecordTurn operation
Define Goalkeeper as a derived Player
Add its SavesPracticed field
Override the inherited RecordTurn contract
The override calls the base body before its own update

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 Violet class diagram containing Player above Goalkeeper.
  2. Put +NumberOfTurns : int and +RecordTurn() : void in Player's attributes and operations. Put +SavesPracticed : int and +RecordTurn() : void in Goalkeeper's corresponding compartments.
  3. Draw a solid generalization connector from Goalkeeper to Player, with a hollow triangle pointing at Player. Do not use a field association or dashed interface realization.
  4. Add notes beside the operation entries: RecordTurn is virtual in C# for Player and RecordTurn overrides Player.RecordTurn and calls the base body first for Goalkeeper.
  5. Save without redeclaring NumberOfTurns inside Goalkeeper. Its instance inherits that field even though the class compartment lists only the derived declaration.
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.

Goalkeeper inherits Player and overrides virtual RecordTurn, calling the base body before updating SavesPracticed.
A derived operation specializes an inherited contract
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

Goalkeeper inherits NumberOfTurns; the class diagram does not need to redeclare that field in the derived compartment. The operation entry in Goalkeeper represents overriding, not another object.

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