Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
scroll top