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.7k18 gold badges210 silver badges308 bronze badges
asked Aug 15, 2011 at 12:45

3 Answers 3

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.

answered Aug 15, 2011 at 12:59
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.

answered Jan 27, 2014 at 14:26
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).
answered Aug 15, 2011 at 17:00
5
  • Are you referring to what is discussed on uml-diagrams.org/class-diagrams.html#dependency? I've never seen the instanceOf stereotype, but I have seen <<creates>> and <<uses>> before. Commented Aug 15, 2011 at 17:12
  • @Thomas Owens: Well, <<instanceOf>> is a well known stereotype since the earliest versions of UML. If you look at the latest stable spec (omg.org/spec/UML/2.3) of the UML infrastructure, you'll find multiple examples of its usage, such as the classical Figure 7.8 about the four-layer metamodel hierarchy that has been used ad nauseam in a multitude of publications. Commented Aug 15, 2011 at 17:27
  • Interesting. I'll have to poke around. Although I'm familiar with UML, I've never dug into the formal spec and all of it's little intricies before. Commented Aug 15, 2011 at 17:36
  • @Thomas Owens: Sure, and good luck. :-) Commented Aug 15, 2011 at 18:24
  • To add a reference: uml-diagrams.org/uml-core.html#instance-specification-core Commented Aug 16, 2011 at 11:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.