UML class diagram: class overriding relationship cardinality - uml

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?

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).

Related

Proof there is no instance for a given UML-Diagram

Given the diagram in the top-right corner, I'm supposed to decide whether there is any valid instance of it. Now the given image is a counterproof by example ('wegen' means 'because of'). The counterproof uses the cardinality ('Mächtigkeit') of the objects.
I don't understand, why for example 2*|A| equals |C|, as in UML, A would be in relation with 2 objects of C (rel1). So for every A there have to be 2 C to make a valid instance. 2*|A| = |C| should therefore be |A| = 2*|C|.
Why is it the other way around?
2*|A| = |C| since there is double the amount of C objects compared to A because each A has two C associated.
|A| = |B| because they have a 1-1 relation
3*|C| = 2*|B| because each C has 3 B and each B has 2 C
(4) and (5) are just substitutions where the last gives a contradiction
q.e.d
P.S. As #ShiDoiSi pointed out there is no {unique} constraint in the multiplicities. This will make it possible to have multiple associations to the same instance. Ergo, you have 1-1 relations. So with that being the case you actually CAN have a valid instantiation of the model.
Now go and tell that to your teacher xD

Interpretation of Multiplicity of a relation

I'm struggling to understand the multiplicity of a relation.
In general how should one interpret this
is this every entity of type P has between a and b entities of type C or between x and y or something else. All explanations I've found so for only adres the cases a,x = 0,1 and b,y = *
It's vice versa. P has x..y entities of type C in access and C has a..b of P.
As a side note: the multiplicity labels should not be placed to hide parts of the association.
Every Association contains two independent statements:
Every instance of P is linked to x..y instances of C
Every instance of C is linked to a..b instances of P
Being linked could mean, that P or C have an attribute of type C or P. This is the most common incarnation of a link, but UML does not prescribe this.

UML diagrams: Showing inheritance of a vector of pointers to a class

How would I go about showing that class A inherits a vector of pointers to Class B? Would I have an inheritance arrowhead to Vector, or to Class B?
There is no such thing as "class A inherits some resource to class B". Either class B inherits from class A - in this case B specializes A (or, alternatively, A generalizes B); or class A and class B share a resource - in this case it's called aggregation.

How to show constants in UML CLASS DIAGRAM?

I can't decide how to show constant class properties in Uml Diagram. Can you suggest me how to do it?
I would do it like this:
CONSTANT_NAME : constant_type = value
E. g.:
PI : double = 3.1415

UML enumeration as a return type

<< enumeration>> E1 | .RED .GREEN .BLUE |
I have the above as an enumeration class in a UML diagram. I associate it with another class say House. I now need a method on House say +getColor() which returns a color from the above enumeration.
How would I depict this in UML?
would it be like : +getColor(): E1 ?
Yes, your suggestion is right and depending on the level of detail, you could also add dependency from House to E1.

Resources