2

I am trying to find out if it is allowed in the UML to change the visibility (access modifier) of an operation when overriding it. For example, in Java it is possible to increase the visibility of an overridden method from protected to public:

public class A {
    protected void doSomething() {
    }
}

public class B extends A {
    @Override
    public void doSomething() {
    }
}

My question is whether it is possible to model the same in the UML:

Example class diagram

So far I couldn't find anything in the official specification that would forbid this, but I'm not sure if I missed anything. Does anyone know if this is allowed?

xoric
  • 51
  • 6
  • 1
    Certainly it is allowed, just as overloading. UML probably even cannot recognize when a field name occurs twice. As UML is programing language agnostic. The above might generate erroneous code for some programming language Cobol or whatever, but for java it looks splendid – Joop Eggen May 08 '19 at 11:39
  • 1
    Thanks for your answer. However, I am a bit confused about your statement that the UML cannot "recognize" when a field name occurs twice. Are you referring to a particular software tool? After all, the UML itself does not contain any logic, but is merely a modeling language for visualization. In my question, I wasn't referring to whether most software tools (Enterprise Architect, MagicDraw, etc.) allow the above example or not, but whether the official UML specification contains any restrictions in this respect. – xoric May 08 '19 at 12:08
  • Ah, I thought you alluded with "whether possible" to some non-syntactical restriction, as obvious your example gives fine XML/XMI. [OMG](https://www.omg.org/spec/UML/2.5.1/) provides UML specifications upto the syntactical level; which is not helpful here. – Joop Eggen May 08 '19 at 12:22
  • 1
    @xoric: You say it yourself: "[UML] is merely a modeling language for visualization". Why do you then think UML would specify *anything* that is related to the semantics of a programming language like the ability to change the visibility of overridden methods or the ability to specify different return types in overloads? – Bart van Ingen Schenau May 08 '19 at 12:23
  • I see your point. However, even if we are talking about visualization, the UML specification does of course contain some restrictions (e.g. which link types are allowed between certain elements or not). Nevertheless, I am more confident now that there are no restrictions on the above topic. Thanks again. – xoric May 08 '19 at 12:56
  • 1
    @JoopEggen : OMG also specifies _semantics_ in its UML specs, not only syntax; see for example my answer to this question. – www.admiraalit.nl May 08 '19 at 13:18

1 Answers1

5

Yes, this is allowed. In UML, this is called 'redefinition'. To make this explicit, you could add a redefines-constraint to the operation in class B, as follows:

+doSomething() {redefines doSomething}

If the signatures of both operations are identical (as in this case), the redefines-constraint can be omitted and redefinition is implicit (see sections 9.4.4 and 9.9.2.7 of UML 2.5.1 specification).

I have been looking for an explicit statement in the UML specifications that says that the visibility of an operation can be changed. I found this sentence in section 9.4.4 (an Operation is a specific kind of Feature):

.. the redefined Feature shall conform to the compatibility constraint on the redefinitions.

I couldn't find a more detailed specification of "compatibility constraint", but I guess that a visibility change does not violate any compatibility constraint.

Section 9.6.3.1 says:

An Operation may be redefined in a specialization of the featuringClassifier. This redefinition may add new preconditions or postconditions, add new raisedExceptions, or otherwise refine the specification of the Operation.

If we assume that 'refine' is a typo and should be replaced by 'redefine', then this seems to allow redefining any aspect of the operation, including its visibility.

For properties, it is specified explicitly in section 9.5.3 of the UML 2.5.1 specification:

The name and visibility of a Property are not required to match those of any Property it redefines.

I guess this also applies to operations, for consistency sake.

www.admiraalit.nl
  • 1,103
  • 5
  • 8
  • Thank you. I don't want to come across as lazy, but some things are really hard to find. The section you have linked explains my question perfectly. – xoric May 08 '19 at 13:17
  • Thanks. Please check this answer as 'accepted', to give me more credit points. – www.admiraalit.nl May 08 '19 at 13:21
  • is an operation (behavioural feature) a property (structural feature) ? – Christophe May 08 '19 at 13:39
  • Perfect, probably also when in the child class the result/parameter type changes. – Joop Eggen May 08 '19 at 13:45
  • @Christophe : good point. I have extended my answer. – www.admiraalit.nl May 08 '19 at 13:51
  • 1
    what about 9.6.3.1: “an operation may be redefined in a specialisation (...) or otherwise redefine the specification of the operation” ? – Christophe May 08 '19 at 14:02
  • @Christophe : Your quote says 'otherwise _redefine_' but the spec says 'otherwise _refine_ the specification of the operation'. I wondered what was meant by 'refine' until I read your comment; now I assume that it's a typo in the spec! If it's indeed a typo in the spec, then yes, it gives us freedom to redefine any aspect of the operation. Thanks, Christophe! – www.admiraalit.nl May 08 '19 at 14:15