How is the Start method from Batchable Interface able to return two different return types? Iterable or QueryLocator.
As far as Function Overloading is concerned, methods can be overloaded based on different input parameters. But its not possible to overload a method based on different Return Types.
What programming feature is used in Batchable Interface?
I tried to create a similar Interface, but its not allowing me to save it.
Best Answer
Simple; the
Database.QueryLocator
implements theIterable
interface, so actually you are always returning an Iterable of some form.You can consider the ability to define your
start
method as returningDatabase.QueryLocator
as using the covariant return type functionality of other object oriented languages. That the Salesforce documentation specifically shows two different signatures is just to help you understand that both options are available. Covariant return types are not supported across the Apex language, but rather in special cases like this.It is worth noting that Apex is not a formally defined language and has some weirdnesses.
For example, it doesn't really support generics (templating) either. What you see is somewhat "smoke and mirrors". To illustrate this, you can define a batch class as implementing
Database.Batchable<SObject>
yet theexecute
method (when processingAccount
records, for example) can receiveList<Account>
and there will be no compilation or runtime error (as long as thestart
method selects Account records).