Private nested Java class in UML diagram
Asked Answered
C

3

25

I have a question regarding UML. I have a class which simply contains an inner class with the private access modifier - cannot be accessed from anywhere else... Normally in order to present an inner class relation I can use a (+) relation like here (InnerOddIterator):

enter image description here

(taken from http://www.uml-diagrams.org/nested-classifier.html)

I have not found anywhere any information about how can clearly emphasize that this class is private. Do you know if such a method exist at all? If yes I'll be grateful you give me some link here or something?

Just to keep things clear, a sample code:

public class DataStrucure {
     // fields, methods, etc
     private class InnerOddIterator{
          // ... 
     };
}
Cleavage answered 26/11, 2014 at 10:17 Comment(3)
See also "private import" in uml-diagrams.org/package-diagrams.html#package-importFrizzell
If you feel that it's important to clearly emphasize that this class is private, consider using a stereotype such as <<private>>. You simply document what your stereotype means, and there you are. Stereotypes are the "official" way to extend the UML specification, and the point about them that is sometimes missed is that anyone can use them to extend the UML any way they like. The fact that some stereotypes ("<<abstract>>" for example) have found their way into general use doesn't change this.Selfcontradiction
See this post for more current information.Selfcontradiction
C
8

First of all: You have something in your code and asking for an UML representation. But, IMHO, you should look at it the other way round: How can that UML-idea be represented in code. (Some programming languages don't even offer private nested classes...).

As for private nested classes: I suggest using a Composition. It is stronger as Association but not as strong as inheritance. And the composed class can not exist without its composer. Pretty much exactly a private nested class.

The drawing is taken from http://www.uml-diagrams.org/association.html: enter image description here

Carminacarminative answered 26/11, 2014 at 10:40 Comment(4)
"Some programming languages don't even offer private nested classes" - good point, right... I've added Java tag to be more specificCleavage
In Java within an instance if the nested inner class you can use this to access the outer instance - they are ultimately bound together - it can not exist without the outer instance. As said IMHO it is exactly a composition.Carminacarminative
By the way it is not that way in C# - there is no automatic binding to an outer instance ...Carminacarminative
@MartinMeeser I agree with your logic and I upvoted your answer. However, be aware that not everyone in the software development community agree that the only way to achieve composition is via private nested classes like you and me.Launder
D
8

From UML point of view. If classifier (Class also) is nested in other class, nesting class plays role of namespace. In this case nested classes are hidden (private) in context namespace. it means, your diagram implicitly defines private inner class definition.

here is part of definition from UML Superstructure section structred classifiers:

"A class acts as the namespace for various kinds of classifiers defined within its scope, including classes. Nesting of classifiers limits the visibility of the classifier to within the scope of the namespace of the containing class and is used for reasons of information hiding. Nested classifiers are used like any other classifier in the containing class."

Datolite answered 26/11, 2014 at 17:22 Comment(2)
I've been studying this a bit more. The "anchor" notation in the OP's diagram is specified in 1.4 but not in 2.x. I did a bit of analyzing of the Namespace metamodel and it looks like the way to go is to put a namespace property on the containing class, put a composition diamond on the containing class's association end, and put the {subsets namespace} property on the nested class end. The subsets property implements the visibility this quote describes. See this for more.Selfcontradiction
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).Doriedorin
E
1

In order to indicate that your inner class is privete the best, for me, is to use - character as depicted below but of course in this case you miss the internal structure of your inner class..enter image description here

Emblem answered 26/11, 2014 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.