2.6.2 Representing Constructors and Parameters in Class Diagrams

Representing Constructors and Parameters in Class Diagrams

A constructor entry describes the information required to create any Player of this design. It belongs with the class operations, not among the stored attributes.

Distinguish parameters from fields

The example requires a name and a jersey number. Assume a positive jersey number; validation is outside this diagram task.

Source meaning and notation
Part Diagram text
Private name field - name : string
Private jersey field - jerseyNumber : int
Public constructor + Player(name : string, jerseyNumber : int)

The names inside parentheses describe temporary inputs. The attribute entries describe storage on each instance. A parameter does not automatically declare a field.

Build the creation contract in Violet
  1. Create one Player class with the two private fields above.
  2. In operations enter + Player(name : string, jerseyNumber : int).
  3. Add + GetName() : string and + GetJerseyNumber() : int for the public queries.
  4. Keep parameter order: text first, integer second. Do not append : void to the constructor.
Player has private name and jerseyNumber fields, a two-parameter constructor, and public name and jersey queries.
Constructor parameters initialize private fields

A particular call such as new Player with Jordan and 7 supplies actual values; those values do not replace the types in the class signature.

Check what the model promises

The later C# lesson supplies the complete definition and shows the assignment from each parameter into its field.

Apply the idea
Check your understanding