Inheritance from base class


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.


See section on extends_clause in the Modelica Grammar.


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
yes yes yes
type yes yes
record yes yes
yes yes
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 2024-07-21T18:15:58Z by OpenModelicaOpenModelica 1.23.1 using GenerateDoc.mos