0

I want to represent the following case using UML class diagram:

  • a class A that has some elements of C
  • a class B that is a subclass of A that contain 1 element of D that is subclass of C

Something like:

Example

Basically (in my mind), the class A has a collection of C but the subclass B only allows (at maximum) one element in that collection that should be instance of D.

What is the best way of representing it?

NSR
  • 1
  • 1
  • Except that you don't show the hierarchical relationship between D and C, I think you got it. – Bruce Aug 18 '16 at 19:05
  • Your connectors are "wrong". The Generalization does not have the edge leading into the triangle. There is no semantics for thick and thin lines (so they should in general have the same width). – qwerty_so Aug 18 '16 at 22:02
  • sorry about error, i forgot the hierarchy between C and D. Now is correct – NSR Aug 19 '16 at 08:25

1 Answers1

2

To change (limit) a cardinality or to specialise the type of element in a subclass you have to use a "redefines" constraint.

In your case your if your attribute in class A is

anAttribute:C[*]

Then in class B you'll have

anAttribute:D[0..1] {redefines anAttribute}

Of course you can use the same on an attribute shown as association (like in your diagram), then just add {redefines anAttribute} near the end of association (and of course association ends should both be named anAttribute then).

Ister
  • 5,958
  • 17
  • 26