11

Given this pseudo code:

class B { }

class A : B
{
    int val;
};

alpha = new A();

What arrow do I draw between alpha and A in a UML class diagram? Is this even something UML is meant to do?

+-------+        +-------+        +-------+
| alpha | --??-- |   A   |------|>|   B   |
|       |        |       |        |       |
+-------+        +-------+        +-------+

In my case, I truly do want to express explicitly that alpha instantiates A. So if there is no way in UML, I'll just make up a way -- which is fine, but I want to know if UML can express it already. My situation is that I'm trying to explain OOP to a friend who is very visual. Therefore, I want to try to visually distinguish the two relationships (inheritance and instantiation) so that he can distinguish it mentally.

Thomas Owens
  • 85,641
  • 18
  • 207
  • 307

3 Answers3

15

In a class diagram, you don't show the act of instantiation. That is something that a sequence or communication diagram would show. Class diagrams show the static structure of a system.

If a class A "depends on" a class B, then you could look at the association and dependency relationships. Dependency is the weaker of the two and is usually used if the other class is a parameter to a method and is not stored as an instance variable. Association is stronger and means that class A contains an instance (or instances) of class B, and is further strengthened by the composition and aggregation relations.

In your example, alpha isn't part of a class. If alpha was an instance variable in some class C, you would connect C to A with an association. If it was a parameter to a method or created inside a method of C and not stored as an instance variable, you would use a dependency line to connect C to A. In all cases, you have the ability to specify directionality and multiplicity.

I think a larger problem is that you appear to be associating UML with "class diagram". The UML 2.2 specification has 14 diagram types. There are 7 structural diagrams, and 7 behavioral diagrams (broken down into 3 behavior diagrams and 4 interaction diagrams). Each diagram shows a particular view of the system. Some aspects of the system are best shown using a particular diagram, or in some cases, a series of diagrams. If you are interested, I would recommend checking out Wikipedia's entry on UML and Martin Fowler's UML Distilled. Both give great overviews of the purpose, strengths, and weaknesses of various diagram types.

Thomas Owens
  • 85,641
  • 18
  • 207
  • 307
6

enter image description here

There is a special stereotype for instantiation dependency in the UML class diagrams. Class INSTANTIATES Class2, if it creates its instances. Exists in UML standard. ( I checked 2.4.1 - contemporary and 2.5 - the next one.)

But alpha in your code is not a class, but an object. They do not appear on the class diagrams. You need UML Object diagram instead. The Class diagram can show only the capability of one class to instantiate another one.

Gangnus
  • 2,817
4

I agree with @Thomas Owens that class diagrams in UML are not meant to show instances in general. However, I must add that UML does provide mechanisms to depict instantiation:

  • the InstanceSpecification element provides a way to model instances, such as alpha in your example.
  • the dependency notation (a dashed arrow with plain arrowhead), flowing from the instance to its type, and adorned with the stereotype <<instanceOf>>, provides a way to model the instantiation relationship between an instance and its classifier(s).
CesarGon
  • 2,941