Private nested Java class in UML diagram

47,866

Solution 1

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."

Solution 2

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

Solution 3

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

Share:
47,866

Related videos on Youtube

radekEm
Author by

radekEm

Updated on November 27, 2020

Comments

  • radekEm
    radekEm over 3 years

    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{
              // ... 
         };
    }
    
    • xmojmr
      xmojmr over 9 years
    • BobRodes
      BobRodes over 9 years
      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.
    • BobRodes
      BobRodes almost 8 years
      See this post for more current information.
  • radekEm
    radekEm over 9 years
    "Some programming languages don't even offer private nested classes" - good point, right... I've added Java tag to be more specific
  • Martin Meeser
    Martin Meeser over 9 years
    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.
  • Martin Meeser
    Martin Meeser over 9 years
    By the way it is not that way in C# - there is no automatic binding to an outer instance ...
  • BobRodes
    BobRodes over 9 years
    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.
  • hfontanez
    hfontanez almost 5 years
    @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.
  • www.admiraalit.nl
    www.admiraalit.nl over 4 years
    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).