.ModelicaReference.'extends'

Information

Inheritance from base class

Examples

class A
  parameter Real a, b;
end A;

class B
  extends A(b=2);
end B;

class C
  extends B(a=1);
end C;

From the example above we get the following instantiated class:

class Cinstance
  parameter Real a=1;
  parameter Real b=2;
end Cinstance;

The ordering of the merging rules ensures that, given classes A and B defined above,

class C2
  B bcomp(b=3);
end C2;

yields an instance with bcomp.b=3, which overrides b=2.

Syntax

class_definition :
   [ encapsulated ]
   [ partial ]
   ( class | model | record | block | connector | type |
     package | function )
   IDENT class_specifier

class_specifier :
   string_comment composition end IDENT
   | "=" base_prefix name [ array_subscripts ] [ class_modification ] comment
   | "=" enumeration "(" ( [enum_list] | ":" ) ")" comment

composition  :
   element_list
   { public element_list |
     protected element_list |
     equation_clause |
     algorithm_clause
   }
   [ external [ language_specification ]
              [ external_function_call ] [ annotation ";" ]
              [ annotation  ";" ] ]

element_list :
   { element ";" | annotation  ";" }

element :
   import_clause |
   extends_clause |
   [ final ]
   [ inner | outer ]
   ( ( class_definition | component_clause) |
     replaceable ( class_definition | component_clause)
        [constraining_clause comment])

extends_clause :
   extends name [ class_modification ]

Description

The name of the base class is looked up in the partially instantiated parent of the extends clause. The found base class is instantiated with a new environment and the partially instantiated parent of the extends clause. The new environment is the result of merging

  1. arguments of all parent environments that match names in the instantiated base class
  2. the optional class modification of the extends clause

in that order.

The elements of the instantiated base class become elements of the instantiated parent class.

The declaration elements of the instantiated base class shall either

Otherwise the model is incorrect.

Equations of the instantiated base class that are syntactically equivalent to equations in the instantiated parent class are discarded. [Note: equations that are mathematically equivalent but not syntactically equivalent are not discarded, hence yield an overdetermined system of equations.]

Since specialized classes of different kinds have different properties, 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 Class package operator function operator function type record operator record expandable connector connector block model class
package yes yes
operator yes yes
function yes yes
operator
function
yes yes yes
type yes yes
record yes yes
operator
record
yes yes
exapandable
connector
yes yes
connector yes yes yes yes yes yes
block yes yes yes
model yes yes yes yes
class yes

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.


Generated at 2018-12-17T10:58:42Z by OpenModelica 1.14.0~dev-43-g4ff0ebd