Templates are defined with respect to a given metaclass and are inherited by and available to the children of the metaclass. In the following example, the definition template defined on the parent metaclass is available to, and used in the evaluation of the content template on the child metaclass.
GTL supports the following OO concepts as part of inheritance:
Polymorphism - Templates are dynamically bound; the choice of the template to be evaluated is made at translation-time. Polymorphism allows template code defined on a classifier to use templates defined on its children (class, interface), the template being used does not have to be defined on the parent metaclass. Coupled with inheritance, this feature helps you share template code.
In the following example, the content of %definition% depends on whether a class or an interface is being processed:
Template overriding - A template defined on a given metaclass may be redefined on a child class. The template defined on the child overrides the template defined on the parent for objects of the child metaclass. You can view the overridden parent using the Go to super-definition command in the child class contextual menu, and specify the use of the parent template with the "::" qualifying operator. For example:
The same template name "isAbstract" is used in two different categories: Classifier and Class. "false" is the original value that has just been overridden by the new "true" value. You retrieve the original value back by using the following syntax: <metaclassName::template>, in this case:
%isAbstract% %Classifier::isAbstract%
Template overloading - You can overload your template definitions and test for different conditions. Templates can also be defined under criteria or stereotypes (see Criteria (Profile) and Stereotypes (Profile)), and the corresponding conditions are combined. At translation-time, each condition is evaluated and the appropriate template (or, in the event of no match, the default template) is applied. For example: