Interface method can be implemented as static

Assume we have some simple interface:

public interface IExecutable {
    void execute();
}

and some class that implements the interface:

public class Executor implements IExecutable {
    public static void execute() {
        System.debug('Executing from a static context...');
    }
}

Believe you or not but Executor class compiled without any errors. That means that abstract interface methods can be implemented as static:

IExecutable exe = new Executor();
Executor.execute(); // Executing from a static context...
exe.execute(); // Executing from a static context...

I didn’t find any information in ApexDocs about such behavior. Is it a bug or (undocumented) intentionally designed behavior?

Answer

According to Chris Peterson, this is apparently a bug that’s been preserved because people are depending on it.

I think this had to be preserved in the new compiler since so many people relied on it (!!!!). It’s certainly something I’d like to remove from the language, but Release Updates are expensive enough it’s not a priority (yet).

Based on that, this is a bug that should not be relied on in production code. If you rely on undocumented behavior, such as this bug, you’re helping contribute to that internal friction that causes bugs to not be fixed, or at least, be severely delayed.

Attribution
Source : Link , Question Author : Oleh Berehovskyi , Answer Author : sfdcfox

Leave a Comment