Home > Diagrams > Class Diagram

Class Diagram Example

The following example shows a class diagram creation using Eclipse 2.1.

We are going to add general Eclipse Preferences, which will be used in our example.
Click on Window>Preferences.

We decided to create a src and a bin output for this project.
Select Java>Build Path and click on Folders checkbox.

We are going to set up general UML preferences.
Click on UML, wait for the launch of EclipseUML.
Select the following options:
Diagram Presentation:

Select Diagram Board menu to activate the following options:

We want to show Public, Protected, Private, Static and Package Attributes.
We also want to show Public and Package Methods.
We unselect the Use property concept checkbox.

We would like to use a Java Perspective.
Click on the top icon to select Java perspective.

We are going to create a new Java Package.
Select src in the Package Explorer New > Package.

Enter the name of the Package.

Select company package in the Package Explorer, open the popup menu New > Other
Select UML Diagrams > UML Class Diagram then click on the next button.

Enter the name of the class diagram file in the File name field.
Enter company, the ucd (UML Class Diagram) extension is related to the class diagram.
Click on the finish button.

The company class diagram is displayed.

Create a New Package.
Select new package in the toolbar and click on the diagram.

Enter the name of the package in the Name field.

Create a new Class Diagram inside the model Package.
Click on model>Open>model.ucd (ucd is the class diagram extension).

Create a New class.
Select New class in the toolbar and click on the diagram.

Enter Person and click on the Finish button.

We are going to add two new Attributes.
Click on Person>New>Attribute.

Enter Attributes properties: "age: int" and "name: String".

We discover that attributes are not visible in the class diagram editor.

We want to show attributes and Methods in the Class diagram.
Select a class and open the popup menu then select View selector.

Select Attributes tab and attributes checkbox in the left pane.

Select Methods tab and Methods checkbox in the left pane.

Create a New class named Employee.
We would like to add inheritance from Employee to Person.
Select Generalization in the Toolbar.

Drag the Inheritance from Employee to Person.

Add "salary: float" attribute to Employee.
Create a New class named Company.

We want to create a new Association between Company and Employee.
Select Association in the Toolbar then drag from Company to Employee.

It is possible to add a name to the Label, but we will not in this example.

Enter the 1st Association End properties.
Select 1...* Multiplicity.

Enter the 2nd Association End properties.

Click on Company and Create two new Attributes: "name: String" and "activity: String".

Create a New class named Project and add an association between Company and Project.

Enter Association Properties.
Label: leave blank.

Enter the 1st Association End properties.

Enter the 2nd Association End properties.
Select Qualifier in the 2nd Association End.
Select the Qualifier checkbox and enter name in the name field and  Attribute String name.

Please check that your Class diagram is the same as below.

We can use colors to have a better understanding of our diagram.
Select in the menu bar Windows > Preferences > UML > Class Diagram > Colors

Colors have been included in the class diagram below.

Add "name: String" new Attribute to Project class.
Use the view selector to hide getters and setters.

Add an Association between Employee and Project.
Select Association and drag from Employee to Project.
In the properties menu:
Connection: leave blank
1st Association End: Name: leader, Dimension 0, Multiplicity 0..1
2nd Association End: Name Project,  Dimension 0, Multiplicity 0..1

We also need to show association members.
Right click inside the diagram editor, making sure that you don't select one of the elements - otherwise you are going to open the element popup menu and not the diagram popup menu.
Select the Show association member checkbox and click on the OK button.

All association attributes are now displayed in the class diagram.

Select an element and right click to open the element popup menu > View Selector
Use view selector (if needed) to display employee, activity and projectmap attributes etc...
Show code. Click on Employee attribute>Open>Source Editor

Type new code:

Copy Java code inside company class.
The marker shows unresolved type.

Correct the compilation error -> Resolve the type.
Place the mouse cursor at the end of "Iterator".
Control + space and select Iterator - java.util

Now you can go back to your class diagram and enjoy modeling!

The class diagram extension is *.ucd.