Wednesday 18 December 2013

Interfaces Overview in AX 2012


An interface is a language feature that helps you to organize and structure your classes and their relationships to one another.
An interface defines a set of methods but does not implement them. To use an interface, a class has to implement the interface. A class that implements the interface must implement all of the methods defined in the interface.
The purpose of defining an interface is to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Use interfaces to:
  • Capture similarities between unrelated classes without artificially forcing a class relationship
  • Declare methods that one or more child classes must implement

You create an interface under the Classes node of the AOT. Defining an interface is similar to creating a new class. An interface definition has two components: the declaration and the body.
X++
interfaceDeclaration
{
    interfaceBody
}
The interface declaration declares various attributes about the interface, such as its name and whether it extends another interface. The interface body contains the method declarations within the interface as shown in the following example.
X++
interface SysDeleteTables
{
    public void doDeleteCustomerTables();
}
NoteNote
Methods in an interface always have the access level of public. However, use of the public keyword is optional in the interface. Every class that implements the interface must explicitly declare the method to be public.

To use an interface, write a class that implements the interface. When a class implements an interface, the class must provide a method implementation for all of the methods declared within the interface as shown in the following example.
class SysDataImport extends SysDataExpImp implements sysDeleteTables
{
// Supply implementation of methods in interface.
void doDeleteCustomerTables()
{
// Implement the method.
}
}
A class that implements an interface inherits all the method declarations in that interface. However, the class needs to provide bodies for those methods.

Ex:- SysSavable class , sysrunable class


1 comment:

  1. Great thing to learn for any X++ developer, Thanks Aslam

    ReplyDelete