40
votes

How do you indicate a generic class or interface in a Class Diagram?

I'm looking for two things really:

  1. One is how do I model generic java classes if i were doing it with pen and paper.
  2. Secondly, what uml tools out there support generic classes/interfaces.

Thanks!

7

7 Answers

41
votes

Usually generics are represented as classifier's template parameter. Here is the example how it looks like in the NetBeans UML Plugin:

enter image description here

17
votes

UML calls them parameterized types - see examples here.

As for tools, I know MagicDraw supports them.

3
votes

One is how do I model generic java classes if i were doing it with pen and paper.

AFAIK there is no specific support for this in UML, the best may be to use a stereotype like <<generic>>. But on paper with pen, there is nothing simpler than writing MYType<T> :-)

Secondly, what uml tools out there support generic classes/interfaces.

Stereotypes are supported by all UML tools I have seen so far. Of course, the level of support may differ; however, since I hardly use any UML tools apart from napkins :-) I don't have much base for comparison here.

2
votes

I'm looking for two things really: One is how do I model generic java classes if i were doing it with pen and paper.

Craig Larman's book Applying UML and Patterns 3e has instructor slides with the Figures from the book. The following is from Figure 15.5:

enter image description here

Secondly, what uml tools out there support generic classes/interfaces.

I like PlantUML, which will produce the following image from

skinparam style strictuml
participant sales as "sales:\nArrayList<Sale>"

enter image description here

For class diagrams, it produces

skinparam style strictuml
hide empty members
class "ArrayList<Sale>" as S
class Store {
Store()
}
Store "1" -> "1" S : "            sales  "

enter image description here

0
votes

In Netbeans The object to use is called "Derivation Classifier". This is the object to use for MyGeneric in Answer from Vitalii Fedorenko

0
votes

One is how do I model generic java classes if i were doing it with pen and paper.

It is generalization arrow, base class, if it is abstract, should be written in italics. Also can be marked with stereotype.

Secondly, what uml tools out there support generic classes/interfaces.

Use http://staruml.io/. Really nice tool, you can manage all project diagrams in one place. At right top corner, there is object/diagram tree, so you can use same classes(other objects) at different diagrams.