21

How to represent the nested class of C++ in UML?

class A {
    class B {

    }
}
www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
Victor S
  • 4,021
  • 15
  • 43
  • 54
  • Question marked as duplicate answers the question in its own question, but uses the 1.4 notation. – BobRodes Jul 06 '16 at 08:12
  • Question marked as duplicate is only about how to mark a nested class as private and does not ask for all possible representations of nested classes. – www.admiraalit.nl Aug 23 '19 at 08:03

3 Answers3

19

Nested class in UML (for any language) can be represented as:

enter image description here

Here

  1. Class Inner1 is nested inside the outer class Outer 1
  2. Classes Inner2, Inner3, Inner4 classes are nested inside Outer2

Reference taken from here

stamhaney
  • 1,246
  • 9
  • 18
14

I had thought that the spec got away from the cross-and-circle notation. So, I did some wandering around in the specs, and couldn't find it in 2.0. I have to conclude that the 2.0 spec no longer supports it. While it's actually specified in v1.4, I looked all through the 2.4.1 spec, and it isn't anywhere to be seen (in fact, the word "anchor" returns 0 results in a document-wide search). I did some other looking around, and here's what I can piece together.

First, I had always understood that nested classes were a means of implementing composition. Furthermore, UML attempts to be implementation-agnostic, and nested classes aren't. (You can create composition in other ways, and not all OO languages support nested classes.) Now, 1.4's explanation includes this:

If Class B is attached to Class A by an “anchor” line with the “anchor” symbol on Class A, then Class B is declared within the Namespace of Class A. That is, the relationship between Class A and Class B is the namespace-ownedElement association.

Ok. Now UML 2.0 says this:

Kernel package represents the core modeling concepts of the UML, including classes, associations, and packages.

Here is a diagram of the Kernel package:

enter image description here

That's pretty abstruse, but have a look at the NamedElement abstract class at the top left. (A "NamedElement" class is an element that has a name.) Notice that Namespace derives from it. Now, notice on the right, directly to the right of the top of the Namespace class, there's another NamedElement class. One of the associations has the {subsets ownedElement} property on it, and a composition diamond on the Namespace end. On the Namespace end, there is the {subsets owner} property.

This means that NamedElement, when in composition association with Namespace, is a subset of Namespace. In other words, the relationship between Namespace and NamedElement is the namespace-ownedElement association described in the 1.4 spec. Therefore, the composition relationship, when adorned with the namespace and ownedElement properties, represents a nested (or inner, or internal, or whatever your favorite coding language calls it) class.

So, I'm going to say that this is the accepted 2.0 way to show nested classes if you are using composition notation. Like this:

enter image description here

Now, another way is to stick the nested class inside the containing class. The notation examples in the spec don't show this AFAICS, but they show it with other NamedElements (packages, components, etc.) so I don't see why you can't.

However, I don't see that the anchor notation is current. xmojmr's favorite site (and a good site, too), www.uml-diagrams.org, has this to say about it:

Now obsolete UML 1.4.2 Specification defined nested class as a class declared within another class and belonging to the namespace of the declaring class. Relationship between those classes was called "namespace owned element association

Nested classifier, e.g. nested class, nested interface, or nested use case could be used like any other classifier but only inside the containing class or interface.

Per UML 1.4.2 a declaring (nesting) class and a nested class could be shown connected by a line, with an "anchor" icon on the end connected to the declaring class. An anchor icon is a cross inside a circle.

UML 2.x specifications - including the recent UML 2.4.1 - describe nesting of classifiers within structured classes without providing explicit notation for the nesting. Note, that UML's 1.4 "anchor" notation is still used in one example in UML 2.4.x for packages as an "alternative membership notation" and without providing any other details or explanations.

I couldn't find that "one example" diagram, so maybe it's still around. But at the very least, the notation seems to be deprecated. I would either use the properties, create a <<nested>> stereotype, or put the nested class inside the owner class.

BobRodes
  • 5,990
  • 2
  • 24
  • 26
  • I don't think your diagram is correct, because `{subsets namespace}` implies that a similar assocation is also present between a superclass of ParentClass and a superclass of NestedClass, with `namespace` being the name of the association end at the NestedClass side. But these classes do not have any superclasses. – www.admiraalit.nl Aug 23 '19 at 07:43
  • The "circle-plus" notation is still valid in UML 2.*. Section 7.4.4.1 of the UML 2.5.1 specification says: Conforming tools may optionally allow the “circle-plus” notation defined in sub clause 12.2.4 to show Package membership to also be used to show membership in other kinds of Namespaces (for example, to show nestedClassifiers). – www.admiraalit.nl Aug 23 '19 at 08:00
  • Alternatively, nested classes may be displayed inside a compartment of the outer class. Section 9.2.4.1 of the UML spec says: If a Classifier has ownedMembers that are Classifiers, a conforming tool may provide the option to show the owned Classifiers, and relationships between them, diagrammatically nested within a separate compartment of the owning Classifier’s rectangle. For example, a compartment showing the contents of the property nestedClassifier for a Class (see 11.4.2) shall be called “nested classifiers;” – www.admiraalit.nl Aug 23 '19 at 08:01
  • @www.admiraalit.nl That's a good point. so perhaps it isn't. Anyway, good find in Section 7.4.4.1. I couldn't find anything about circle notation in the 2.4.1 spec. Perhaps it was added in the 2.5.1 spec, although of course I may also have missed it. It might be helpful to post another answer, citing the 2.5.1 spec quotes that you're using, and adding an example diagram of your own. In any case, thanks for pointing this out. – BobRodes Aug 23 '19 at 17:35
  • I can't add an answer. The question is closed. – www.admiraalit.nl Aug 23 '19 at 19:09
  • @www.admiraalit.nl Yeah, I guess it is. Got marked as a duplicate (probably incorrectly, as you say). Perhaps you can add an answer to the canonical question. – BobRodes Aug 23 '19 at 19:35
  • The question was reopened and I have added my answer here. – www.admiraalit.nl Aug 26 '19 at 09:24
8

Nested classes may be displayed inside a compartment of the outer class.

nested-classes

Section 9.2.4.1 of the UML version 2.5.1 specification says:

If a Classifier has ownedMembers that are Classifiers, a conforming tool may provide the option to show the owned Classifiers, and relationships between them, diagrammatically nested within a separate compartment of the owning Classifier’s rectangle. (...) For example, a compartment showing the contents of the property nestedClassifier for a Class (see 11.4.2) shall be called “nested classifiers”.

Alternatively, nested classes may be displayed using the "circle-plus" notation:

cpn

Section 7.4.4.1 of the UML 2.5.1 specification says:

Conforming tools may optionally allow the “circle-plus” notation defined in sub clause 12.2.4 to show Package membership to also be used to show membership in other kinds of Namespaces (for example, to show nestedClassifiers).

(I have copied the image from the answer posted by @stamhaney)

www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32