Implementing Interfaces

Overview

In order to support object-oriented programming, a function block can implement several interfaces which allows you to use methods.

Syntax

FUNCTION_BLOCK <function block name> IMPLEMENTS <interface_1 name>|,<interface_2 name>, ..., <interface_n name>

A function block that implements an interface must contain all methods and properties (interface properties) defined by this interface. This includes name, inputs, and the output of the particular method or property which must be exactly the same.

For this purpose - when creating a new function block implementing an interface - automatically all methods and properties defined in this interface will be inserted below the new function block in the Applications Tree.

NOTE: If afterwards, methods are added to the interface definition, they will not be added automatically in the concerned function blocks and thus produce compiler errors. Execute the command Implement interfaces... to perform this update explicitly.

When you use inheritance for function blocks, consider that if methods or attributes were created by inheritance of an interface, either implement them, or delete them if the implementation of the base function block is to be used. Pragma attributes are automatically inserted. They are detected during compilation and messages are generated to remind you that the inherited methods or properties have to be verified. Delete the pragma attributes after you have completely implemented the new function block.

For further information, refer to the description of the Implement interfaces... command.

Example

INTERFACE I1 includes method GetName:

METHOD GetName : STRING

Function blocks A and B each implement interface I1:

FUNCTION_BLOCK A IMPLEMENTS I1
FUNCTION_BLOCK B IMPLEMENTS I1

Thus in both function blocks the method GetName has to be available and will be inserted automatically below each when the function blocks are inserted in the Applications Tree.

Consider a declaration of a variable of type I1:

FUNCTION DeliverName : STRING 
VAR_INPUT 
  l_i : I1; 
END_VAR 

This input can receive all function blocks that implement interface I1.

Example for function calls:

DeliverName(l_i := A_instance); // call with instance of type A 
DeliverName(l_i := B_instance); // call with instance of type B 
NOTE: A variable of an interface-type must get assigned an instance of a function block before a method can be called on it. A variable of an interface-type always is a reference to the assigned function block instance.

Thus a call to the interface method results in a call to the function block implementation. As soon as the reference is assigned, the corresponding address is monitored in online mode. Otherwise, if no reference has been assigned yet, the value 0 is displayed within monitoring in online mode.

For this example see in the implementation part of the function DeliverName:

DeliverName := l_i.GetName(); // in this case it depends on the "real" type of l_i whether A.GetName or B.GetName is called
NOTE: See also the possibility to extend a function block by using the keyword EXTENDS in the declaration.