1

Is there a way I can implement something like an abstract class with a generic type? For example, I want to be able to write code like the following (I know there are issues there). The main error I get is, "type parameters not supported".

Ultimately I'd love to have a way to extend an abstract class and easily customize the concrete class to be specific to one particular Sobject type.

LeadAssignJob class

global class LeadAssignJob extends AbstractAsyncJob<Lead> {
    ...
    global override Boolean evaluate(List<Lead> records) { ... }
    ...
}

AbstractAsyncJob class

global abstract AbstractAsyncJob<T> {

    global abstract Boolean evaluate(List<T> records);

    global void process(List<T> newRecords, Map<Id, T> oldRecordsById) {
        ...
        if (evaluate(newRecords)) { ... }
        ...
    }
}

LeadTrigger

trigger LeadTrigger on Lead (after insert, after update) {
    new LeadAssignJob.process(Trigger.new, Trigger.oldMap);
}
1

They experimented with Parameterized Classes/Interfaces, but this feature was deprecated about four years ago. They've hinted at the idea that they might revive this feature at some point in the future, but we're not holding our breath. In the meantime, you'll have deal with just naming your classes and pseudo-parameterization:

public abstract class A {
  public abstract void evaluate(SObject[] records);
}

...

public class B extends A {
  void evaluate(Lead[] records) {
    ...
  }
  public override void evaluate(SObject[] records) {
    evaluate((Lead[])records);
  }
}

This is awfully annoying, but workable'ish. You might want to suggest an idea, and let me know so I can go vote, too.

| improve this answer | |
  • Thanks sfdcfox... I found an existing idea, with just one vote from the poster, lol... – Marty C. May 19 '17 at 17:18
  • @MartyC. Huh. I know that I would use them like... all the time. They're so incredibly useful. I have probably a dozen things I'd go use them for immediately if they suddenly became available. – sfdcfox May 19 '17 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.