Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UML Representing an anonymous class in class diagram?

Tags:

java

oop

uml

How would one represent a Java anonymous class in a UML class diagram?

like image 618
user997112 Avatar asked Jul 28 '12 22:07

user997112


Video Answer


2 Answers

Inner (nested) classes are represented in UML with an association adorned with a crossed circle.

Illustration:

UML inner class

Source:

  • UML Class Diagrams for Java Programmers, by Robert C. Martin on InformIT.
like image 187
Eliran Malka Avatar answered Nov 05 '22 13:11

Eliran Malka


There are two really anonymous classes in Java First is the non-named inner class. Such as:

class BGThread<T>{...}
...
class TitleEditDlg{
    new BGThread<Props>(cont, true) {
        @Override
        public Props run() {
            ...
        }
    }
 }

A citation from UML standard 2.5 (p.149):

The standard notation for an anonymous InstanceSpecification of an unnamed Classifier is an underlined colon (‘:’).

So, as for anonymous java class, you should create a class block with only : as name and connect the container class to it twice - by container relationship and by one-direction arrow without a dot. From the other side, the : block should connect to the parent class.


According to the same source, an Anonymous Bound Class, that is the second anonymous class we meet in Java, but often do not notice it, when you use a template/generic class, as in

class BGThread<T>{...}
...
class TitleEditDlg{
   BGThread<String> newThread= new BGThread<String>();
}    

can be shown by two ways:

  • As bind dependency, with substitution on it.
  • As an intermediate class, with the name of the parent class and substitution in angle brackets. Notice, here the class is anonymous, but the attribute has a name. So, this way you are showing more information.

enter image description here

like image 26
Gangnus Avatar answered Nov 05 '22 13:11

Gangnus