In short
Mainstream class-based OO languages do not allow the class of an object to change after creation. I assumed the same constraint in UML, based inter alia on the following clauses in the UML 2.5 specification:
7.5.1: Types and multiplicity are used in the declaration of Elements that contain values, in order to constrain the kind and number of values that may be contained.
9.2.3.2: An instance of a Classifier is also an (indirect) instance of each of its generalizations.
However, in the comments to this answer, several highly skilled UML experts questioned this assumption. Therefore my question: can instances change class in UML? What clauses in the UML specs would allow such a change?
Additional arguments
While I couldn't find any firmer claus in the UML specs about the relation between instances and classes, I interpreted the following hints to support the fixed class:
11.4.3.1: (...) An active object is an object that, as a direct consequence of its creation, commences to execute its classifierBehavior, and does not cease until either the complete Behavior is executed or the object is terminated by some external object.
(It's specific for active objects. But if the object could change class after its creation, it could not be active anymore, or it would keep the old behavior with the new class, which seems weird):
In addition there are multiple clauses that would lead to ambiguity, if the class of the instance could change dynamically. For example in 6.5.1:
A class may also have invariant conditions that must be true before and after the execution of the operation but may be violated during the course of the execution of the operation method.
What happens if the invariant is no longer true during an operation on a classifier instance: does the instance just looses its quality of instance of the class? COuld it be an instance not related to any class at all?
The semantics of properties specify that, when a property with a default value is instantiated, in the absence of some specific setting for the property, the default value is evaluated to provide the initial values of the property
If an instance would no longer be an instance of one class with a default value, would it suddenly get the default of another class it could be instance of? Or if an instance could match two similar classes: would it get the defautl value of both?
Fruit
intoCar
? None, I guess. Think of what such a change would have in consequences. Different attributes and operations. That would just be insane. I'll try to foster the UML spec, but right from my stomach that's just bullshit. P.S.Object
is the term from UML 1.5. Now that's anInstanceSpecification
. – DecedentObject
and usingInstanceSpecification
instead. An object can be anything it is classified with (hence the possibility to change). But only in this frame it's allowed to change. It can not change arbitrarily into "something". – Decedent