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?

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 8, 2019 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 8, 2019 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 provides UML specifications upto the syntactical level; which is not helpful here.
    – Joop Eggen
    May 8, 2019 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? May 8, 2019 at 12:23
  • 1
    @JoopEggen : OMG also specifies semantics in its UML specs, not only syntax; see for example my answer to this question. May 8, 2019 at 13:18

1 Answer 1

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.

7
  • 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 8, 2019 at 13:17
  • Thanks. Please check this answer as 'accepted', to give me more credit points. May 8, 2019 at 13:21
  • is an operation (behavioural feature) a property (structural feature) ?
    – Christophe
    May 8, 2019 at 13:39
  • Perfect, probably also when in the child class the result/parameter type changes.
    – Joop Eggen
    May 8, 2019 at 13:45
  • 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 8, 2019 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.