What is a stereotype and what are its purpose?
A stereotype extends UML by allowing to categorize some elements in a more detailed way than foreseen natively by UML. It helps to make the model more expressive.
A typical example are the Entity, Control, and Boundary stereotypes. They are used in a use-case driven design: in a class diagram, you can immediately find-out which classes represents the use-case logic (marked as «control»
stereotype), which classes represents user-interface with actors («boundary»
stereotype), and which classes represents business/domain objects (marked as «entity»
).
Another use of stereotypes is to indicate the way a class is supposed to be implemented, for example to mark as «table»
classes that correspond to database tables that are related to in-memory objects, or to indicate among the objects which ones are «persistent»
. An even more common use is to show some concepts that are language specific and have no equivalent in UML. I think in particular to C# «struct»
which have a value semantic wheres C# classes have a reference semantic, or C# «property» (which is a stereotype at attribute level).
How are stereotypes defined?
Sterotypes are defined in a profile. You can see a stereotype as a class in a profile diagram.
But it's not an ordinary class that you could relate to other classes in your model. So there is no inheritance at play here. You would never indicate a superclass as stereotype.
A stereotype is a class related to the "metamodel", which means the UML standard elements defined in the UML standard. So it's a class of classes (or a class of associations, or of any other UML element).
To elaborate on your example, with an abstract superclass Animal
and concrete sub-classes Cat
and Dog
:
- you could affect them all an
«entity»
profile, because they all belong to the domain model.
- if you would want to store the objects in an RDBMS using the concrete table inheritance, you may use a stereotype «persistent» for
Cat
and Dog
(since concrete table inheritance pattern does not implement persistence for the abstract classes)