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.
- User Define Interface (Custom Interface)
- 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:- Database. Batchable Interface (For Handling bulk records)
- System. Schedulableinterface (For Running apex class at a specific time)
- Site.UrlRewriter (Enables rewriting Sites URLs.)
- HttpCalloutMock (Enables sending fake responses when testing HTTP callouts.)
- WebServiceMock Interface (Enables sending fake responses when testing Web service callouts of a class auto-generated from a WSDL.)
- Messaging.InboundEmailHandler interface (To handle an inbound email message.)
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