Search code examples
umlclass-diagram

How do Cardinalities and Generalization work in UML?


I am trying to understand how Cardinalities and Relationships work with Generalization in simple UML class diagrams. I have this simple example here:

enter image description here

A Player owns exactly 9 Fields, but the distribution of them is irrelevant (i.e. 5 Endfields, 4 Startfields or 1 Endfield and 8 Startfields). The Field class is abstract. If i model this problem as seen above, would a Player have 9 Fields from each type? If so, how do i model this correctly?


Solution

  • If i model this problem as seen above, would a Player have 9 Fields from each type?

    not at the same time. In your model a Player is associated to 9 Fields, knowing each Field is a Endfield or-exclusive a Startfield (supposing there are no other classes inheriting Field)

    A given instance of Field cannot be both a Endfield and a Startfield, else that means Fields inherits both Startfield and Endfield (but the inheritances are in the opposite direction)

    The corresponding classes in Java from your model can be :

    abstract class Field { .... }
    class Startfield extends Field { ... }
    class Endfield extends Field { .. }
    

    so a Player can be in relation with 9 Endfields, or-exclusive 8 Endfields and 1 Startfield, or-exclusive 7 Endfields and 2 Startfield, ..., or-exclusive 1 Endfield and 8 Startfields, or-exclusive 9 Startfields

    If you want a Player has 9 Startfields and 9 EndFields your model can be : enter image description here


    [edit]

    However if you extend your model adding an other class inheriting Startfield and Endtfield like :

    enter image description here

    in case a Player is associated to 9 Bothfields in a way it is also associated with 9 Startfield and 9 Endtfield

    The multiplicity (9) of the relation concerns Field, for the inheriting classes that depends as you can see