Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

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.

share|improve this answer
add comment

enter image description here

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

share|improve this answer
add comment

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).
share|improve this answer
    
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. –  Thomas Owens Aug 15 '11 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. –  CesarGon Aug 15 '11 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. –  Thomas Owens Aug 15 '11 at 17:36
    
@Thomas Owens: Sure, and good luck. :-) –  CesarGon Aug 15 '11 at 18:24
    
To add a reference: uml-diagrams.org/uml-core.html#instance-specification-core –  Alexander Bird Aug 16 '11 at 11:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.