X++ programming language is a class based object oriented programming language and it allows developers to use abstract types.
One of such an abstract types that can be used in Dynamics AX is an interface. Interface allows us to detach contract from the class implementation, such an abstraction is very useful tool. Interface basically says that “hey, we want this class to implement such a methods”. For example we could imagine that we would want to have a car that would have an ability to drive, we could for instance implement an interface Drivable:
interface Drivable
{
void moveForth()
{
}
void moveBack()
{
}
void turnRight()
{
}
void turnLeft()
{
}
}
Having such an interface we can implement it on Car (concrete class) making Car class obliged to provide an implementation for the methods specified by the interface Drivable:
class Car implements Drivable
{
str model;
void moveForth()
{
/* Implementation needed */
}
void moveBack()
{
/* Implementation needed */
}
void turnRight()
{
/* Implementation needed */
}
void turnLeft()
{
/* Implementation needed */
}
private void otherMethod()
{
/* some additional functionality */
}
}
UML diagram for this configuration looks as follows:
We can also provide multiple interfaces like for example in SysTestCase class:
class SysTestCase extends SysTestAssert
implements SysTestable, SysTestSuiteProvider, SysTestSuiteActor, SysTestableExceptionExpected, SysTestListenerInformationProvider, SysTestableFaultExpected
Three things that developer needs to remember implementing interfaces:
1. Class that implements interfaces needs to provide implementation for all the methods from all the interfaces.
2. Access level defined by interface cannot be restricted by derived class.
3. In other languages for example Java interfaces could contain constant declarations (eg. static methods), but in X++ we do not have such an ability.
Interfaces describe relationship “has-a”, so when you design one it can be useful to ask yourself “does that class has a property of …” for example “does this Car has a property of being Drivable” if yes then it should implement interface Drivable, this is why most of the interfaces in standard Dynamics implementation like SysTestable and SysComparable have names ending on –able. Unfortunately there is one significant drawback to using interfaces, namely they are not as flexible as using inheritance for example. Once the interface is implemented making modifications becomes hard and any addition to the interfaces at this point needs additional development.