Inheritance principle
Content of the inheritance
A derived function block inherits all data, methods, properties, actions and transitions, which are defined in the basic function block. Note the access options to inherited elements, which are defined via access modifiers.
Access options to inherited elements
To what extent a subclass can access inherited methods or properties within its scope depends on the access modifier, with which the method or property is defined in the base class.
Methods and properties, which are defined in the base class with the access modifier PRIVATE, cannot be called within the scope of the subclass, nor can they be overwritten or extended by the subclass.
Private methods and properties are available for the subclass only in so far as that they are executed for the instance of the subclass, if they are called within the implementation of the base class.
Example:
The base class has a PUBLIC and a PRIVATE method. The PUBLIC method calls the PRIVATE method in its implementation. The PUBLIC method can be called by the subclass called, so that the PRIVATE method is called implicitly at the same time. However, the PRIVATE method cannot be actively called, overwritten or extended by the subclass.
The following access modifiers available for specifying access options to a method or property:
PUBLIC | Corresponds to the specification of no access modifier. The element (method or property) can be called from outside the function block. Therefore, the element can also be accessed by a subclass. |
PRIVATE | Access to the element is limited to the function block. Access from outside the function block is not possible. This means that a subclass cannot access the element either. The subclass can therefore neither call the element, nor overwrite or extend it. |
PROTECTED | Access to the element is limited to the function block and its derivatives. A subclass can access the element and can therefore call, extend or overwrite it. Access from outside this "inheritance family" is not possible. |
INTERNAL | Access to the element is limited to the namespace (the library). Access from outside the namespace is not possible. Therefore, the element cannot be overwritten or extended from outside the namespace. |
Extending or overwriting of inherited elements
- A derived function block can extend or overwrite the methods, properties, actions and transitions defined in the basic function block, if a corresponding access modifier for the elements is used in the base class.
- In order to be able to extend or overwrite an element, the element must be declared in the subclass in the same way as in the base class:
- same name
- same access modifier (for methods and properties)
- same variable interface (e.g. method inputs/outputs)
- same return type (for methods and properties)
- When an element is extended or overwritten, in the subclass only the implementation part is adjusted, in order to adjust the behavior of the element.
Engineering tip: The following engineering support is provided for extending or overwriting methods, properties, actions and transitions inherited by the function block: When you add a method, property etc. to the derived function block, the Name of the Add dialog (e.g. Add Method, Add Property) offers a drop-down list with a choice of methods, properties etc. used in the basic function block. If, for example, you select one of these methods in the Add Method dialog, the other declaration settings for the method (return type, access modifier) are automatically applied to the method declaration of the base class. When you confirm the dialog, the method is created according to these declarations. You can customize the implementation part of the method such that it matches the desired behavior of the subclass.
Further information on extending and overwriting can be found in section “Use cases for inherited elements”.
Further considerations relating to extensions
- An instance of the derived function block can be used in any context in which TwinCAT expects a function block of the type of the basic function block.
- A derived function block must not contain any function block variables with the same names as declared by the basic function block. The compiler reports this as an error.
The only exception: If you have declared a variable as VAR_TEMP in the basic function block, the derived function block may define a variable with the same name. The derived function block can then no longer access the variable of the basic function block. - Within the scope of the derived function block, the variables and elements (e.g. methods, properties, actions, transitions) of the basic function block can be addressed directly by using the SUPER pointer.