2

I have created the following interface and a class that implements it.

global interface SampleInterface {
    String logicA();
}

public class SampleClass implements SampleInterface {
    public String logicA();
} 

global class SampleBatch implements Database.Batchable<SObject> {

    private SampleInterface interface;

    global SampleBatch(SampleInterface interface) {
        this.interface = interface;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        // I want to set a variable for SampleClass
        this.interface.aaa = 'aaa';

        this.interface.logicA();
    }

}

I want to set a variable for SampleClass

Even though I can add public string aaa; in SampleClass, within SampleInterface I could not could not include String aaa ;.

How can I pass parameters to SampleClass?

3

If you want a little more flexibility, you may wish to switch to an abstract class instead. Doing so will allow you to specify variables as well, and even include some method definitions if you so desire.

public abstract class Demo
{
    public Integer foo;
    public abstract String bar();
    void baz()
    {
        system.debug(foo);
    }
}

Then your implementations become extensions:

public with sharing class Sample extends Demo
{
    public Sample(Integer foo)
    {
        this.foo = foo;
    }
    public override String bar()
    {
        return 'LIMIT ' + foo;
    }
}

Your Answer

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