Use cases for inherited elements
In general, inherited elements to which the subclass has appropriate access (see section "Access options to inherited elements") can be used in three different ways:
- Inherited elements can be used unchanged.
- Inherited elements can be overwritten.
- Inherited elements can be extended.
These three use cases are explained below using the example of the "Method" element.
Unchanged use
- Requirement: The subclass requires exactly the same implementations that have already been programmed in the basic class method.
- Implementation: In this case, the method is not created for the subclass.
- Consequence: The subclass uses the method implementation of the base class.
- Example:
- The base class must control an axis for executing a process.
- The same requirement applies to the subclass: the subclass must also control the axis.
- In this case, the method ExecuteProcess is not created for the subclass. If the method is called for an instance of the subclass (fbSub.ExecuteProcess (...)), the basic implementation of the method is called automatically (FB_Base.ExecuteProcess). As a result, the subclass benefits from the implementations that have already been implemented in the base class.
Function block FB_Base:
FUNCTION_BLOCK FB_Base
VAR
fbAxis : FB_Axis;
END_VAR
Method FB_Base.ExecuteProcess:
METHOD ExecuteProcess : BOOL
VAR_INPUT
bExecuteProcess : BOOL;
END_VAR
// Calling axis module by passing input parameter "bExecuteProcess" of this method to the input parameter "bExecute" of method "Execute"
fbAxis.Execute(bExecute := bExecuteProcess);
// Setting the return value of this method as inverted error signal of the axis module
ExecuteProcess := NOT fbAxis.Error;
Function block FB_Sub:
FUNCTION_BLOCK FB_Sub EXTENDS FB_Base
VAR
END_VAR
Method FB_Sub.ExecuteProcess:
[does not exist]
Overwrite
- Requirement: Compared with the base class, the subclass requires completely different instructions in the method.
- Implementation: In this case, the method for the subclass is created and filled with other instructions in the implementation part. Compared with the base class method, only the implementation part differs – the declaration part must be identical.
- Consequence: The subclass uses its own implementation of the method. The subclass has overwritten the base class method.
- Example:
- The base class must control an axis for executing a process.
- In contrast, the subclass does not have to control an axis but a cylinder during the process execution.
- In this case, the method ExecuteProcess is created for the subclass. The implementation part of the method is programmed with the required instructions, which have a completely different effect compared to the basic implementation.
Function block FB_Base:
FUNCTION_BLOCK FB_Base
VAR
fbAxis : FB_Axis;
END_VAR
Method FB_Base.ExecuteProcess:
METHOD ExecuteProcess : BOOL
VAR_INPUT
bExecuteProcess : BOOL;
END_VAR
// Calling axis module by passing input parameter "bExecuteProcess" of this method to the input parameter "bExecute" of method "Execute"
fbAxis.Execute(bExecute := bExecuteProcess);
// Setting the return value of this method as inverted error signal of the axis module
ExecuteProcess := NOT fbAxis.Error;
Function block FB_Sub:
FUNCTION_BLOCK FB_Sub EXTENDS FB_Base
VAR
fbCylinder : FB_Cylinder;
END_VAR
Method FB_Sub.ExecuteProcess:
METHOD ExecuteProcess : BOOL
VAR_INPUT
bExecuteProcess : BOOL;
END_VAR
// Calling cylinder module by passing input parameter "bExecuteProcess" of this method to the input parameter "bExecute" of method "Execute"
fbCylinder.Execute(bExecute := bExecuteProcess);
// Setting the return value of this method as inverted error signal of the cylinder module
ExecuteProcess := NOT fbCylinder.Error;
Extension
- Requirement: The subclass requires both the implementation as already implemented in the base class, as well as additional instructions that are specific to the subclass.
- Implementation: In this case, the method for the subclass is created and filled with the additionally required instructions in the implementation part. At the desired position within the subclass method, the base class method is called via SUPER^.SampleMethod (...). This call executes the original base class method. The additional instructions within the subclass method also execute additional instructions that are specifically required for the subclass.
- Consequence: The subclass uses its own additional implementation as well as the implementation of the base class (by calling the SUPER pointer). The subclass has extended the base class method.
- Example:
- The base class must control an axis for executing a process.
- The subclass must also control an axis during process execution. In addition, the subclass must control a cylinder.
- In this case, the method ExecuteProcess is created for the subclass. The implementation part of the method is programmed with the required additional instructions. The base class method (FB_Base.ExecuteProcess) is called at a suitable point in the sequence of the subclass method using the SUPER pointer (SUPER^.ExecuteProcess (...)). As a result, the subclass benefits from the implementations that have already been implemented in the base class. In addition, it can extend or customize the implementations with instructions that are required by the subclass.
Function block FB_Base:
FUNCTION_BLOCK FB_Base
VAR
fbAxis : FB_Axis;
END_VAR
Method FB_Base.ExecuteProcess:
METHOD ExecuteProcess : BOOL
VAR_INPUT
bExecuteProcess : BOOL;
END_VAR
// Calling axis module by passing input parameter "bExecuteProcess" of this method to the input parameter "bExecute" of method "Execute"
fbAxis.Execute(bExecute := bExecuteProcess);
// Setting the return value of this method as inverted error signal of the axis module
ExecuteProcess := NOT fbAxis.Error;
Function block FB_Sub:
FUNCTION_BLOCK FB_Sub EXTENDS FB_Base
VAR
fbCylinder : FB_Cylinder;
END_VAR
Method FB_Sub.ExecuteProcess:
METHOD ExecuteProcess : BOOL
VAR_INPUT
bExecuteProcess : BOOL;
END_VAR
// Extension: Calling cylinder module by passing input parameter "bExecuteProcess" of this method to the input parameter "bExecute" of method "Execute"
fbCylinder.Execute(bExecute := bExecuteProcess);
// Setting the return value of this method as inverted error signal of the cylinder module PLUS calling the base method and analyzing its return value
ExecuteProcess := NOT fbCylinder.Error AND SUPER^.ExecuteProcess(bExecuteProcess := bExecuteProcess);