Search
Close this search box.
What Is an Interface In Apex Salesforce and How to Use It

What Is an Interface In Apex Salesforce and How to Use It

An interface is like a class that does not have any of its methods implemented. Each method’s body is empty; all that is present are the method signatures. Because they isolate the precise implementation of a method from its declaration, interfaces can be used to add an abstraction layer to your code. As a result, depending on particular conditions, you can have different implementations of a method. All of the methods contained in an interface’s body must be implemented in the apex class for it to be used in a class. An apex interface is created using the interface keyword.

Another method for achieving polymorphism and abstraction in Apex is through an interface. Contracts are similar to interfaces. A class can only have a declaration added; the actual implementation cannot. You might be wondering why we need to create a class with nothing in it. We’ll consider this after looking at the next case.

				
					// Interface
public interface className{
   // Method's signature
   String methodName();
}

				
			

Let’s take an example company named Atrium. Suppose that we need to return the employee levels of associates, consultants and managers. We will create an interface called AtriumEmployee

				
					 // Interface
public interface AtriumEmployee{
   String employeeLevels(); // method signature only
}
// Managers Employee Class
public class Managers implements AtriumEmployee{  
   //Method Call
   public String employeeLevels() { 
      // For Managers, levels should be L4/L5
      return “ L4/L5 “;
   }
}
// Consultant Employee Class
public class Consultant implements AtriumEmployee{
      // Method Call
   public String employeeLevels() { 
      // For Consultant , levels should be L2/L3
      return “L2/L3“;
   }
}

				
			

You can see that both the managers and consultant classes implement the AtriumEmployee interface, so the AtriumEmployee interface body must be defined in the employeeLevels method of the class. 

Any methods in the interface must be implemented by a class when it implements our interface. With the use of interfaces, you may give your code structure by making some methods necessary. 

In Salesforce, there are some common interfaces, such as the schedulable interface that Salesforce provides. You must create an apex class that implements the schedulable interface in order to schedule an apex class to execute at the scheduled time.

There is another method in the scheduler that has to be used: execute ().

				
					global class scheduledMerge implements Schedulable {
        global void execute(SchedulableContext SC) {
     //Scheduled code
   }
}

				
			

Important Notes:

  1. A function that has been used must be made public or global.
  2. All of the methods listed in the interface must be implemented by any apex classes that use them.
  3. The implementation of a particular method is separated from its definition by an interface. As a result, various implementations of the same procedure are possible. The idea behind interfaces is that they allow you to change implementations without having to alter your entire code. As a result, the method signature (return type, parameters) never changes.
  4. Interface is a new data type that can be used. There are a few predefined interfaces in apex, including String, Integer, Double, and others. In other words, Salesforce can modify how those APIs are implemented without changing our code.
  5. You can’t add a method to a global interface after the class has been uploaded in a Managed – Released package version.

When and where to use an interface:

  1. Before you begin working on the implementation of specific methods, you can supply the interface to another team. They don’t have to wait for your code. Thus, no more blocks!
  2. When a new data type needs to be created using a particular set of methods but with a different implementation.
  3. A wise choice if the API doesn’t change frequently.
  4. When you need to ensure a certain logic.

 

Atrium is a trusted Salesforce partner. Learn more about our partnership and services to see how we can help you.