Muokkaa

Jaa


Extending interfaces in AL

APPLIES TO: Business Central 2024 release wave 2 and later.

An interface in AL is a syntactical contract that can be implemented by a nonabstract method. The interface is used to define which capabilities must be available for an object, while allowing actual implementations to differ, as long as they comply with the defined interface. For more information, see Interfaces in AL.

With Business Central 2024 release wave 2, extensible interfaces in AL are supported. Extending interfaces enables you to create a flexible and adaptable extension. You can add new functionality without altering the core system, which can save time and resources and you reduce the risk of introducing errors into the existing codebase.

When you declare an interface, you can extend one or more existing interfaces. The new interface will inherit all the methods from the interfaces it extends. So, when you implement an interface that extends other interfaces, the implementing class or object must provide implementations for all the methods defined in the extended interfaces, in addition to any methods defined in the new interface itself. This ensures that the implementor adheres to the contract specified by all the interfaces involved.

Syntax

In the example, TheImplementor can be used as both IFoo, IBar, and IFooBar. The syntax for extending an interface is as follows:

interface IFoo
{
    procedure Foo();    
}

interface IBar
{
    procedure Bar();
}

interface IFooBar extends IFoo, IBar
{
    procedure FooBar();
}

codeunit 10 TheImplementor implements IFooBar
{
    // Must implement IFoo, IBar, IFooBar 
}

Extensible interfaces and operators

Extensible interfaces in AL also support the use of the testing and casting operators is and as. The is operator allows you to check if an object implements a specific interface, while the as operator enables you to cast an object to a specific interface type. This functionality enhances the flexibility and robustness of your code because it allows dynamic type checking and casting. Learn more in Type testing and casting operators for interfaces.

Interfaces in AL
Type testing and casting operators for interfaces