About Me

My photo
PLANO, Texas, United States

Monday, August 18, 2014

Interfaces in Apex

An interface is like a class in which none of the methods have been implemented—the method signatures are there, but the body of each method is empty. To use an interface, another class must implement it by providing a body for all of the methods contained in the interface.

Interfaces can provide a layer of abstraction to your code. They separate the specific implementation of a method from the declaration for that method. This way you can have different implementations of a method based on your specific application.

In Apex, there are two types of Interface.
  1. User Define Interface (Custom Interface)
  2. System define Interface (Standard Interface)

Use Define Interface

Developer can develop the Interface via apex programming Language and can implement it in the other apex class using implements keyword.

Below is the example of an Interface define by the User:

//Interface with define two method
//Start with prefix I to denote interface
public interface ICountNumber{
    Integer getSum(integer a,integer b);
    Integer getMulti(integer a,integer b);
}

Apex class that implements the above interface:

public class clsCountNumber implements CountNumber
{
    public Integer getSum(Integer a,Integer b)
    {
        return (a+b);
    }
    public Integer getMulti(Integer a,Integer b)
    {
        return (a*b);
    }
}

Systems defined Interface (Standard Interface)

Salesforce provides some standard interface that we can implement In your apex classes. Some of the Standard Interface as below:
  1. Database. Batchable Interface (For Handling bulk records)
  2. System. Schedulableinterface  (For Running apex class at a specific time)
  3. Site.UrlRewriter (Enables rewriting Sites URLs.)
  4. HttpCalloutMock (Enables sending fake responses when testing HTTP callouts.)
  5. WebServiceMock Interface (Enables sending fake responses when testing Web service callouts of a class auto-generated from a WSDL.)
  6. Messaging.InboundEmailHandler interface (To handle an inbound email message.)
There are many other interfaces provided by Salesforce but the above are generally used.


1 comment:

  1. Thanks for sharing and explaining me in detail about the latest Salesforce Course in Chennai. Hope waiting for more posts like this. Salesforce Training

    ReplyDelete