.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 clauses of another kind of specialized class:

Base Class
Derived Class package function type record connector block model
package yes
function yes
type yes
record yes
connector yes yes yes
block yes yes
model yes yes yes

The specialized classes package, function, type, and record 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).


Generated at 2016-04-28T14:04:17Z by OpenModelica1.9.3+dev (r25613)