1

I have a Class A which is the super class of the class B (that is public class B extends A). Now I have a another class C, Class A is a instance variable in class C and class B is being downcasted from Class A and being assign to a local variable in class C. How should I represent this relationship in a uml class diagram?

Christophe
  • 68,716
  • 7
  • 72
  • 138
Rui
  • 117
  • 2
  • 12
  • 1
    The statement is badly formulated. "*Class A is a instance variable in class C*" does that mean the class *C* has a non static *attribute* of type *A* ? "*a local variable in class C*" is that again an *attribute* of class *C* or a 'real' *local variable/parameter* in an operation of *C* ? "*class B is being downcasted from Class A*" a class cannot be 'downcasted', the statement globally confuses class and instance of class. Anyway "*class B is being downcasted from Class A and being assign to a local variable in class C*" this is a behavior and cannot be shown in a class diagram. – bruno Jun 28 '20 at 07:29

1 Answers1

1

The class-diagram is straightforward:

enter image description here

Tha association of C with A is structural, since there is an instance variable of that type.

The fact that an operation of C performs a downcast from the instance variable to B in a local variable, does not change the class diagram: the class diagram is about the structure, and not about what may happen temporary during the execution of the one or the other methods.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Thanks for your reply, but what confused me is since B (from downcasting) is being assign to a local variable in class C, does that mean B and C have dependency relationship? – Rui Jun 29 '20 at 14:06
  • 1
    @Rui Indeed! I was hesitating to mention the `«use»` dependency in my answer because this dependency could be an unfortunate implementation detail and not a design intention. In theory a different implementation could be imagined, e.g. avoid the downcast, or extract that part into a helper. Furthermore while the implementation of C depends on B, the class C itself doesn’t (i.e. you wouldn’t need to include B.h in C.h since B is not used as parameter nor as return value). For all these reasons I’d not show dependency. Show it only if it’s required by design or if it’s an implementation model. – Christophe Jun 29 '20 at 14:30