Demonstration of Access Control


Members of a Modelica class can have two levels of visibility:
public or protected. The default is public if nothing else is specified. Publically declared variables can be can be read or updated by any code with access to that class. Variables that are declared protected can be reched only by code inside the class as well as code in classes that inherit this class. However, only code inside the class is allowed access to the same instance of a protected variable.You can read about this in Private and Protected Elements.


1 AccessDemo

The variables a, x, z and u3 is public and w, u and u2 are protected in the AccessDemo class



2 Simulation of AccessDemo




3 A

In A an illegal modification is done, since p2 is protected it can not be modified if AccessDemo is not inherited. Additionally, accessing x2 via dot notation ad.x2 is illegal since x2 is a protected variable.



4 Simulation of AccessDemo, A and B

Here we can see that if we try to simulate class A with the erroneous modification"p2 = 3.5" we get a warning for trying to modify a protected variable.





5 B

In the class B AccessDemo is inherited, then it is legal to change the value p2.



6 Simulation of B