One of the major benefits of object-orientation is the ability to extend the behavior and properties of an existing class. The original class, known as the base class, is extended to create a more specialized version of that class, known as the derived class or subclass. In this process, the data and behavior of the original class in the form of variable declarations, equations, and certain other contents are reused, or inherited, by the subclass. In fact, the inherited contents is copied from the superclass into the subclass, but before copying certain operations, such as type expansion, checking, and modification, are performed on the inherited contents when appropriate. This chapter describes the inheritance concept in Modelica, together with the related concepts modification and redeclaration.
The extends-clause is used to specify inheritance from a base class into an (enclosing) class containing the extends-clause. The syntax of the extends-clause is as follows:
The name of the base class is looked up in the partially flattened enclosing class (section 5.2) of the extends-clause. The found base class is flattened with a new environment and the partially flattened enclosing class of the extends-clause. The new environment is the result of merging
arguments of all enclosing class environments that match names in the flattened base class
the optional class-modification of the extends-clause
in that order.
[Example:
]
The elements of the flattened base class become elements of the flattened enclosing class, and are added at the place of the extends-clause: specifically components and classes, the equation sections, algorithm sections, optional external clause, and the contents of the annotation at the end of the class, but excluding import-clauses.
[From the example above we get the following flattened class:
The ordering of the merging rules ensures that, given classes A and B defined above,
yields an instance with bcomp.b=3, which overrides b=2.]
The declaration elements of the flattened base class shall either
Not already exist in the partially flattened enclosing class [i.e., have different names] .
The new element is a long form of redeclare or uses the ‘class extends A’ syntax, see section 7.3.
Be exactly identical to any element of the flattened enclosing class with the same name and the same level of protection (public or protected) and same contents. In this case, the first element in order (can be either inherited or local) is kept. It is recommended to give a warning for this case; unless it can be guaranteed that the identical contents will behave in the same way.
Otherwise the model is incorrect.
[Clarifiying order:
]
Equations of the flattened base class that are syntactically equivalent to equations in the flattened enclosing class are discarded. This feature is deprecated, and it is recommend to give a warning when discarding them and for the future give a warning about all forms of equivalent equations due to inheritance. [Note: equations that are mathematically equivalent but not syntactically equivalent are not discarded, hence yield an overdetermined system of equations.]
Multiple inheritance is possible since multiple extends-clauses can be present in a class.
If an extends-clause is used under the protected heading, all elements of the base class become protected elements of the current class. If an extends-clause is a public element, all elements of the base class are inherited with their own protection. The eventual headings protected and public from the base class do not affect the consequent elements of the current class (i.e., headings protected and public are not inherited).
Since specialized classes of different kinds have different properties, see section 4.6, only specialized classes that are “in some sense compatible” to each other can be derived from each other via inheritance. The following table shows which kind of specialized class can be used in an extends clause of another kind of specialized class (the “grey” cells mark the few exceptional cases, where a specialized class can be derived from a specialized class of another kind):
Base Class | ||||||||||||
Derived | package | operator | function | operator | type | record | operator | expandable | connector | block | model | class |
Class | function | record | connector | |||||||||
package | yes | yes | ||||||||||
operator | yes | yes | ||||||||||
function | yes | yes | ||||||||||
operator | yes | |||||||||||
function | yes | yes | ||||||||||
type | yes | yes | ||||||||||
record | yes | yes | ||||||||||
operator | yes | |||||||||||
record | yes | |||||||||||
expandable | yes | |||||||||||
connector | yes | |||||||||||
connector | yes | yes | yes | yes | yes | yes | ||||||
block | yes | yes | yes | |||||||||
model | yes | yes | yes | yes | ||||||||
class | yes |
If a derived class is inherited from another type of specialized class, then the result is a specialized class of the derived class type. [For example, if a block inherits from a record , then the result is a block.]
All specialized classes can be derived from class [provided the resulting class fulfills the restriction of the specialized class. It is recommended to use the most specific specialized class.] A class may only contain class-definitions, annotations, and extends-clauses (having any other contents is deprecated).
The specialized classes package, operator, function, type, record, operator record, and expandable connector can only be derived from their own kind [(e.g. a package can only be base class for packages. All other kinds of classes can use the import statement to use the contents of a package)] and from class.
[Examples:
]
The class name used after extends for base-classes and for constraining classes must use a class reference considered transitively non-replaceable, see definition in section 6.2.1. [This formulation excludes the long form of redeclare, i.e. ‘redeclare model extends M...’ where M must be an inherited replaceable class.] For a replaceable component declaration without constraining clause the class must use a class reference considered transitively non-replaceable. [This implies that constraining classes are always transitively non-replaceable – both if explicitly given or implicitly by the declaration.]
There are three kinds of constructs in the Modelica language in which modifications can occur:
Variable declarations.
Short class declarations.
Extends-clauses.
A modifier modifies one or more declarations from a class by changing some aspect(s) of the declarations. The most common kind of modifier just changes the default value or the start value in a binding equation; the value and/or start-value should be compatible with the variable according to section 6.6.
[Example: Modifying the default start value of the altitude variable:
]
A modification (i.e. C1 c1(x = 5) is considered a modification equation, if the modified variable is a non-parameter (here: c1.x) variable. [This equation is created, if the modified component (here: c1) is also created (see section 4.5). In most cases a modification equation for a non-parameter variable requires that the variable was declared with a declaration equation, see section 4.7; in those cases the declaration equation is replaced by the modification equation.]
A more dramatic change is to modify the type and/or the prefixes and possibly the dimension sizes of a declared element. This kind of modification is called a redeclaration (section 7.3) and requires the special keyword redeclare to be used in the modifier in order to reduce the risk for accidental modeling errors. In most cases a declaration that can be redeclared must include the prefix replaceable (section 7.3). The modifier value (and class for redeclarations) is found in the context in which the modifier occurs, see also section 5.3.1.
[Example: Scope for modifiers
]
The syntax is defined in the grammar, section B.2.5.
The modification environment contains arguments which modify elements of the class (e.g., parameter changes). The modification environment is built by merging class modifications, where outer modifications override inner modifications. [Note: this should not be confused with inner outer prefixes described in section 5.4]
Merging of modifiers means that outer modifiers override inner modifiers. The merging is hierarchical, and a value for an entire non-simple component overrides value modifiers for all components, and it is an error if this overrides a final prefix for a component, or if value for a simple component would override part of the value of a non-simple component. When merging modifiers each modification keeps its own each-prefix.
[The following larger example demonstrates several aspects:
The following example demonstrates overriding part of non-simple component:
The modification environment of the declaration of t is (x=3). The modification environment is built by merging class modifications, as shown by:
Outer modifications override inner modifications, e.g., b=66 overrides the nested class modification of extends C2(b=6). This is known as merging of modifications: merge((b=66), (b=6)) becomes (b=66).
A flattening of class C4 will give an object with the following variables:
Variable | Default value |
x1 | none |
x2 | 22 |
x3.a | 33 |
x4.b | 4 |
x4.c | 44 |
x5.a | x3.a |
a | 55 |
b | 66 |
c | 77 |
]
Two arguments of a modification shall not modify the same element, attribute, or string-comment. When using qualified names the different qualified names starting with the same identifier are merged into one modifier. If a modifier with a qualified name has the each or final-prefix that prefix is only seen as applied to the final part of the name.
[Example:
The following examples are incorrect:
The following examples are correct:
]
The following rules apply to modifiers:
The each keyword on a modifier requires that it is applied in an array declaration/modification, and the modifier is applied individually to each element of the array (in case of nested modifiers this implies it is applied individually to each element of each element of the enclosing array; see example). If the modified element is a vector and the modifier does not contain the each-prefix, the modification is split such that the first element in the vector is applied to the first element of the vector of elements, the second to the second element, until the last element of the vector-expression is applied to the last element of the array; it is an error if these sizes do not match. Matrices and general arrays of elements are treated by viewing those as vectors of vectors etc.
If a nested modifier is split, the split is propagated to all elements of the nested modifier, and if they are modified by the each-keyword the split is inhibited for those elements. If the nested modifier that is split in this way contains re-declarations that are split it is illegal.
[Example:
This implies that c[i].a[j]=j, and c[i].d=i.
This implies that b.c[i].a[j]=2+j and b.c[i].d=1+i
This implies that b[k].c[i].a[j]=j and
For ’c.a’ the additional (outer) each has no effect, but it is necessary for ’c.d’.
]
An element defined as final by the final prefix in an element modification or declaration cannot be modified by a modification or by a redeclaration. All elements of a final element are also final. [Setting the value of a parameter in an experiment environment is conceptually treated as a modification. This implies that a final modification equation of a parameter cannot be changed in a simulation environment].
[Examples:
]
A redeclare construct in a modifier replaces the declaration of a local class or component with another declaration. A redeclare construct as an element replaces the declaration of a local class or component with another declaration. Both redeclare constructs work in the same way. The redeclare construct as an element requires that the element is inherited, and cannot be combined with a modifier of the same element in the extends-clause. For modifiers the redeclare of classes uses a special short-class-definition construct; that is a subset of normal class definitions and semantically behave as the corresponding class-definition.
A modifier with the keyword replaceable is automatically seen as being a redeclare.
In redeclarations some parts of the original declaration is automatically inherited by the new declaration. This is intended to make it easier to write declarations by not having to repeat common parts of the declarations, and does in particular apply to prefixes that must be identical. The inheritance only applies to the declaration itself and not to elements of the declaration.
The general rule is that if no prefix within one of the following groups is present in the new declaration the old prefixes of that kind are preserved.
The groups that are valid for both classes and components:
public, protected
inner, outer
constraining type according to rules in section 7.3.2.
The groups that are only valid for components:
flow, stream
discrete, parameter, constant
input, output
array dimensions
Note that if the old declaration was a short class definition with array dimensions the array dimensions are not automatically preserved, and thus have to be repeated in the few cases they are used.
Replaceable component array declarations with array sizes on the left of the component are seen as syntactic sugar for having all arrays sizes on the right of the component; and thus can be redeclared in a consistent way.
[Note: The inheritance is from the original declaration. In most cases replaced or original does not matter. It does matter if a user redeclares a variable to be a parameter and then redeclares it without parameter.]
[
Example of arrays on the left of the component name:
]
A class declaration of the type ‘redeclare class extends B(...)’ , where class as usual can be replaced by any other specialized class, replaces the inherited class B with another declaration that extends the inherited class where the optional class-modification is applied to the inherited class. [Since this implies that all declarations are inherited with modifications applied there is no need to apply modifiers to the new declaration.] Inherited B here means that the class containing redeclare class extends B(…) should also inherit another declaration of B from one of its extends-clauses. The new declaration should explicitly include redeclare.
For ‘redeclare class extends B(...)’ the inherited class is subject to the same restrictions as a redeclare of the inherited element, and the original class B should be replaceable, and the new element is only replaceable if the new definition is replaceable. In contrast to normal extends it is not subject to the restriction that B should be transitively non-replaceable (since B should be replaceable).
The syntax rule for class extends construct is in the definition of the class-specifier nonterminal (see also class declarations in section 4.5):
The nonterminal class-definition is referenced in several places in the grammar, including the following case which is used in some examples below, including package extends and model extends:
[Example to extend from existing packages:
Example for an advanced type of package structuring with constraining types:
Note, since MostAir extends from PartialMedium, constant nX=2 in package MoistAir and the model BaseProperties and the function dynamicViscosity is present in MoistAir. By the following definitions, the available BaseProperties model is replaced by another implementation which extends from the BaseProperties model that has been temporarily constructed during the extends of package MoistAir from PartialMedium. The redeclared BaseProperties model references constant nX which is 2, since by construction the redeclared BaseProperties model is in a package with nX = 2.
This definition is compact but is difficult to understand. At a first glance an alternative exists that is more straightforward and easier to understand:
Here, the usual approach is used to extend (here from PartialMedium) and in the modifier perform all redeclarations. In order to perform these redeclarations, corresponding implementations of all elements of PartialMedium have to be given under a different name, such as MoistAir2.MoistAir_BaseProperties, since the name BaseProperties already exists due to extends PartialMedium. Then it is possible in the modifier to redeclare PartialMedium.BaseProperties to MoistAir2.MoistAir_BaseProperties. Besides the drawback that the namespace is polluted by elements that have different names but the same implementation (e.g. MoistAir2.BaseProperties is identical to MoistAir2.MoistAir_BaseProperties) the whole construction does not work if arrays are present that depend on constants in PartialMedium , such as X[nX]: The problem is that MoistAir_BaseProperties extends from PartialMedium.BaseProperties where the constant nX does not yet have a value. This means that the dimension of array X is undefined and model MoistAir_BaseProperties is wrong. With this construction, all constant definitions have to be repeated whenever these constants shall be used, especially in MoistAir_BaseProperties and MoistAir_dynamicViscosity. For larger models this is not practical and therefore the only practically useful definition is the complicated construction in the previous example with redeclare model extends BaseProperties.
To detect this issue the rule on lookup of composite names (section 5.3.2)
ensures that
PartialMedium.dynamicViscosity is incorrect in a
simulation model.
]
In a replaceable declaration the optional constraining-clause defines a constraining type. Any modifications following the constraining type name are applied both for the purpose of defining the actual constraining type and they are automatically applied in the declaration and in any subsequent redeclaration. The precedence order is that declaration modifiers override constraining type modifiers.
If the constraining-clause is not present in the original declaration (i.e., the non-redeclared declaration):
The type of the declaration is also used as a constraining type.
The modifiers for subsequent redeclarations and constraining type are the modifiers on the component or short-class-definition if that is used in the original declaration, otherwise empty.
The syntax of a constraining-clause is as follows:
[Example of merging of modifiers:
A modification of the constraining type is automatically applied in subsequent redeclarations:
A modification of the base type without a constraining type is automatically applied in subsequent redeclarations:
Circuit2 is intended to illustrate that a user can still select any resistor model, including the original one - as is done in Circuit3, since the constraining type is kept from the original declaration if not specified in the redeclare. Thus it is easy to select an advanced resistor model, without limiting the possible future changes.
A redeclaration can redefine the constraining type:
]
The class or type of component shall be a subtype of the constraining type. In a redeclaration of a replaceable element, the class or type of a component must be a subtype of the constraining type. The constraining type of a replaceable redeclaration must be a subtype of the constraining type of the declaration it redeclares. In an element modification of a replaceable element, the modifications are applied both to the actual type and to the constraining type.
In an element-redeclaration of a replaceable element the modifiers of the replaced constraining type are merged to both the new declaration and to the new constraining type, using the normal rules where outer modifiers override inner modifiers.
When a class is flattened as a constraining type, the flattening of its replaceable elements will use the constraining type and not the actual default types.
The number of dimension in the constraining type should correspond to the number of dimensions in the type-part. Similarly the type used in a redeclaration must have the same number of dimensions as the type of redeclared element.
[Examples:
In these examples the number of dimensions must be the same in T1 and T2, as well as in a redeclaration. Normally T1 and T2 are scalar types, but both could also be defined as array types – with the same number of dimensions. Thus if T2 is a scalar type (e.g. type T2= Real) then T1 must also be a scalar type; and if T2 is defined as vector type (e.g. type T2=Real[3]) then T1 must also be vector type.]
Description and annotations on the constraining-clause are applied to the entire declaration, and it is an error if they also appear on the definition. [The intent is that the description and/or annotation are at the end of the declaration, but it is not straightforward to specify this in the grammar.]
[Examples:
]
See also the examples in section 7.3.4.
The following additional constraints apply to redeclarations (after prefixes are inherited, section 7.3):
Only classes and components declared as replaceable can be redeclared with a new type [redeclaration with the same type can be used to restrict variability and/or change array dimensions], which must have an interface compatible with the constraining interface of the original declaration, and to allow further redeclarations one must use “redeclare replaceable”
an element declared as constant cannot be redeclared
an element declared as final may not be modified, and thus not redeclared
Modelica does not allow a protected element to be redeclared as public, or a public element to be redeclared as protected.
Array dimensions may be redeclared; provided the sub-typing rules in section 6.3 are satisfied. [This is one example of redeclare of non-replaceable elements.]
A declaration can have an annotation “choices” containing modifiers on choice, where each of them indicates a suitable redeclaration or modifications of the element.
This is a hint for users of the model, and can also be used by the user interface to suggest reasonable redeclaration, where the string comments on the choice declaration can be used as textual explanations of the choices. The annotation is not restricted to replaceable elements but can also be applied to non-replaceable elements, enumeration types, and simple variables. For a Boolean variable, a choices annotation may contain the definition checkBox = true, meaning to display a checkbox to input the values false or true in the graphical user interface.
Choices menus of replaceable elements can be automatically constructed showing the names of all classes that are either directly or indirectly derived by inheritance from the constraining class of the declaration. This can be recommended by having annotation choicesAllMatching = true; and disabled by having annotation choicesAllMatching = false. [The behavior when choicesAllMatching is not specified; ideally it should present (at least) the same choices as for choicesAllMatching = true; but if it takes (too long) time to present the list it is better to have choicesAllMatching = false.]
[Example:
It can also be applied to nonreplaceable declarations, e.g. to describe enumerations.
It can also be applied to Boolean variables to define a check box.
]