9

This is purely for academic purpose to understand how interfaces work

I found many examples where you pass something while implementing an interface

 global class CustomIterable 
       implements Iterator<Account>

global class CleanUpRecords implements Database.Batchable<sObject> 

what exactly does <account> and <sobject> stand here for? Can someone post a custom interface sample code which requires such a parameter to be passed(so that its more clear)

  • Anyone any idea? – sfdc99999 Jul 9 '14 at 13:28
  • Did you find out what you wanted to? – Alex Tennant Jul 28 '14 at 13:38
14

Parametrised interfaces were removed in Winter '13 (Release Notes, page 191), and you cannot create them in code saved with API version 26 or above. However they are still used by a number of internal Salesforce interfaces, such as Database.Batchable.

Parametrised interfaces (and classes) are common in other OO-languages such as C# and Java, where they are referred to as generics and are much more flexible (C# documentation, Java documentation).

There is still some old database.com documentation available which explains how to create and use them.

Parameterized Typing and Interfaces

Parameterized typing allows interfaces to be implemented with generic data type parameters that are replaced with actual data types upon construction.

The following gives an example of how the syntax of a parameterized interface works. In this example, the interface Pair has two type variables, T and U. A type variable can be used like a regular type in the body of the interface.

 public virtual interface Pair<T, U> {
      T getFirst();
      U getSecond();
      void setFirst(T val);
      void setSecond(U val);
      Pair<U, T> swap();
 }

The following interface DoubleUp extends the Pair interface. It uses the type variable T:

public interface DoubleUp<T> extends Pair<T, T> {}

Implementing Parameterized Interfaces

A class that implements a parameterized interface must pass data types in as arguments to the interface's type parameters.

public class StringPair implements DoubleUp<String> {
    private String s1;
    private String s2;

   public StringPair(String s1, String s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

   public String getFirst() { return this.s1; }
   public String getSecond() { return this.s2; }

   public void setFirst(String val) { this.s1 = val; }
   public void setSecond(String val) { this.s2 = val; }

   public Pair<String, String> swap() {
        return new StringPair(this.s2, this.s1);
    }
}
8

We can't implement custom parameterized interfaces. By way of example, the code for the first interface in the question would look like this:

public interface Iterator<T> {
    boolean hasNext();
    T next();
}

In short, T is a placeholder for another type of data that is substituted during compilation time. For example, Iterator<Account> makes a virtual interface:

public interface Iterator<Account> {
    boolean hasNext();
    Account next();
}

This is a common design pattern in languages similar to Apex Code, such as Java and C#.

Interfaces with parameters allows the same interface be used for any data type. Without parameters we must write the same code differing only by data type for each type we want to support, or limit ourselves to using only Object, which isn't type safe.

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.