UML class diagram: is this how to write abstract method and property?
Asked Answered
D

4

26

When I was creating the first time an uml class diagram for a small C# project I had some trouble with the properties. At the end I just added the properties as a variable with <<property>> at the start. Now Im wondering how do I solve this with an abstract method? Should I just add <<abstract>> to the method an fine? Im doing something like this:

-----------------------------------
|           <<abstract>>          |
|             MyClass             |
-----------------------------------
|<<property>> + a : int           |
|<<property>> + b : string        |
-----------------------------------
|<<abstract>> # Job() : void      |
|<<abstract>> # Job2() : string   |
|- SomeNonAbstractMethod() : void |
-----------------------------------

Is this alright? Any suggestions?

Darrel answered 28/9, 2012 at 8:43 Comment(0)
N
25

Every "attribute" is actually a Property in UML2. Abstract methods are displayed by italic text (UML has a boolean value for this).

The notation you are using is called Keyword (simple way) or Stereotype (more complex). If you want to mark a Property as some kind of "special" that's fine with a keyword like you did.

Nehemiah answered 28/9, 2012 at 9:6 Comment(0)
P
10

If you're using UMLet to make the UML class you can simply put your class between two forward slashes:

UML abstract class with an abstract method using UMLet

Philanthropist answered 24/3, 2021 at 13:40 Comment(0)
I
1

In UML 2.5.1, released 2017, a Class has Properties (typically a "property" in many programming languages) and Operations (reoughly a "method" in many programming languages).

Abstract Operations

The C# "methods" Job and Job2 are Abstract Operations.

An Operation (section 9.6) is a kind of BehavioralFeature. A BehavioralFeature can be abstract (section 9.9.2.5). A BehavioralFeature is itself an abstract Class (section 9.9.2.5). Every Class is an EncapsulatedClassifier (section 11.4.1). Every EncapsulatedClassifier is a StructuredClassifier (11.3.1). Every StructuredClassifier is a Classifier (section 11.1). I quote the standard now (from section 9.2.4.1):

The name of an abstract Classifier is shown in italics, where permitted by the font in use. Alternatively or in addition, an abstract Classifier may be shown using the textual annotation {abstract} after or below its name

In all cases, MyClass, Job and Job2 are all abstract classifiers, and the preferred way to show this is by italic font. And/or use notation {abstact}. The notation <<abstract>> comes from an older UML standard.

Properties

a and b are Properties owned by the Class MyClass. Such an owned Property is also called an Attribute. (11.4.3.1). Section 11.4.4 states about notation

A Class has four mandatory compartments: attributes, operations, receptions (see 9.2.4) and internal structure (see 11.2.4).

One can read more about compartments in section 9.2.4. The take home message there is: The fact that a and b are properties is conveyed by simply placing them in the topmost compartment on the Class box - the "attributes" box. This is exactly what you did. Writing <<property>> could be omitted.

The notation <<property>> means that the Properties a and b follow a stereotype called property. It is not defined in UML, but can be part of some UML Profile. According to https://stackoverflow.com/a/63784017 , this is a specific C# stereotype. So I guess you can keep the notation you have, indicating that a and b are not only UML Properties, but also C# property.

Interstitial answered 4/11, 2022 at 15:43 Comment(0)
K
0

As there are no properties in UML I think this is a possible solution to emphasize that an attribute shall be implemented as property. However you should document the usage of this non-standard keyword within the document you use the diagram.

Another solution would be to create a convention that all public attributes must be implemented as properties (unless some exceptions occur ... ).

If I remember correctly abstract methods are displayed by using italic text. I do not like this approach though, because it might be overseen more easily than in your diagram. It also might depend on the possibilities your UML editor offers, where I usually prefer to adapt the method of the editor to keep in line with other diagrams drawn with the same editor.

Knighterrant answered 28/9, 2012 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.