4

As a follow up to this question I posted yesterday, I have a generic batch job class as so:

public abstract class GenericJob implements Database.Batchable<SObject>, Database.Stateful {
    String query;
    
    public void setQuery(String query) {
        this.query = query;
    }
    
    public String getQuery()
    {
        return query;
    }
    
    public Database.QueryLocator start(Database.BatchableContext context) {
        return Database.getQueryLocator(query);
    }
    
    public abstract void execute(Database.BatchableContext context, List<sObject> scope);
    
    public void finish(Database.BatchableContext context) {
        ... // my own finish logic here
        doFinish(context);
    }
    
     public abstract void doFinish(Database.BatchableContext context); // user defined execute logic
}

As you can see, I have the finish function where I want to carry out some work of my own, and then give the user the option to perform their own finish logic after I'm done with mine. I would like to allow the user to override the doFinish function, but how do I prevent them from overriding the finish (or any of the other functions for that matter)? It seems that overriding any method is just a step away with the override keyword. I can't make the functions private as they are overriding the same function declarations of the Database.Batchable interface.

Do I just hope and pray that my client will have the good sense to know what to leave alone? Or is there a way I can actually prevent them from overriding that method unknowingly?

I haven't found a solution to this anywhere. Any help would be appreciated. Thank you!

2 Answers 2

5

You can only override functions defined as virtual or abstract. The way your class is written, the finish method cannot be overridden, despite the class being an abstract class.

3
  • Understood. Thanks! The reason I posted this question was due to a misunderstanding. Do see my answer for details [salesforce.stackexchange.com/a/156148/40851].
    – coldspeed
    Jan 17, 2017 at 5:02
  • Should I delete this question?
    – coldspeed
    Jan 17, 2017 at 5:03
  • @Shiva Leave that to the community to decide. I'm not sure if this question/answer is a duplicate or not, thinking about it. It's worth having around.
    – sfdcfox
    Jan 17, 2017 at 5:04
0

Okay, so the reason I posted this question in the first place is that, when I tried overriding it, the developer console did not throw an error at first. So I believed that this was actually a problem.

When I refreshed the console and tried recompilation, the console then threw the following error:

Job1: Non-virtual, non-abstract methods cannot be overriden: void Finish(Database.BatchableContext)

Conclusion: If a method in an abstract class is non-virtual, it cannot be overridden at all.

Hope this helps anyone having this query.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .