[SalesForce] How to override an inherited static remote method in APEX or VisualForce

I have a parent class that provides a set of remote action methods that are common to my other controllers:

global virtual with sharing class MyBaseClass {
    @RemoteAction
    global static void remoteMethodA () {
        // stuff
    }
    @RemoteAction
    global static void remoteMethodB () {
        // stuff
    }
}

and child classes that inherit the base class. One of the child classes needs a different implementation of one of the remote action methods:

global with sharing class MyChildClass extends MyBaseClass {
    @RemoteAction
    global static void remoteMethodA () {
        // this is different from MyBaseClass
    }
}

Per Salesforce's Remote Methods and Inheritance, static remote action methods are "inherited"* by child classes. The problem is that Salesforce doesn't know not to inherit remoteMethodA in MyChildClass, and the Visualforce remote object throws an error on page load:

Visualforce Remoting: Unable to create method 'remoteMethodA': already exists on object 'MyChildClass'

It seems to default to using the child class remote action method, as the page works properly, but I would like to clear up the error if I can. Is there a way to tell Visualforce that the child class method overrides the parent class method?

* Static methods are not inherited in any OOP sense – Visualforce walks up the chain of inheritance to build a list of available remote actions.

Best Answer

I guess you have two options:

  1. Raise a bug on the developer forums to see if Salesforce will specifically handle the case where both the parent and child class define the same static remote action method.
  2. Rename the child static method to something else, such as remoteMethodC

I'd recommend the second approach for a few reasons:

  1. It will be clearer to others that the child class is doing something different to the remote action to the base class. Developers probably wouldn't immediately go looking for the definition of the static method in both classes.
  2. You aren't gaining much by using the same method name. It's not like the usage will be polymorphic, as the Visualforce page would need an explicit reference to the child class. Any code reuse would require the child class to call the base classes static method. This isn't dependent on the child class method name.
  3. You won't need to raise a bug with Salesforce support and wait for it to be resolved.
Related Topic