Tip for overriding methods

  • April 19, 2010

There is a simple and generic way to force overriding a method you created. To do so you just need create your new method and place a ‘throw error’ statement in it. To finish you can add the static method missingOverride from the error class and the funcName() to your error. Now when this method is called or the super() in you child class is called, you will get a error that you need to override your method.

public identifiername myMethod()
    throw  error(Error::missingOverride(funcName()));

When you use this peace of code you will get the following error:

Note: The error-class also has some other usefull static methods like: missingFormActiveBuffer, missingOverload, missingOverride, missingParameter, missingRecord and my favorite wrongUseOfFunction

  1. Björn left a comment on April 20, 2010 at 7:10 am

    Why not use the abstract keyword (and abstract base class) if an implementation is required anyway? This makes the compiler check for missing methods and there’s no chance of errors at runtime.

  2. Jeroen Doens left a comment on April 20, 2010 at 8:30 am

    Forcing it with the keyword abstract is indeed a better way because of the check on compile time.

  3. Luegisdorf left a comment on April 21, 2010 at 11:32 am

    I think, the shown way with the exception throw is historically used in AX, when the abstract mofier was not active (AX 2.5).

Comments are closed.