Interfaces in Salesforce Apex

Interfaces in Salesforce Apex

On January 22, 2024, Posted by , In Salesforce Apex Tutorial, With Comments Off on Interfaces in Salesforce Apex
Interfaces in Salesforce Apex
Interfaces in Salesforce Apex

Table of Contents

What is an Interface?

An Interface is similar to a class having methods without a body that means only method signature is available in the Interface but the body of the methods is empty. To use an Interface in class, the body of all the methods contained in the interface must be implemented in the Apex class. Interface keyword is used to create an apex interface.

Why Interfaces in Apex?

Interfaces are used to provide abstractions to your code. They separate the particular implementation for a method from the declaration for the method. In this manner, you can provide different implementations of a method for different applications.

Interfaces in Apex example

Let’s take a simple example of an interface and how we can implement it in different apex classes. Suppose a company has two types of purchase orders, one from customers and the other from employees. So, the company has to provide discounts based on the type of purchase order.

Here, we will create a DiscountPurchaseOrder Interface

// An interface that defines what a purchase order looks like in general
public interface DiscountPurchaseOrder {
    // All other functionality excluded
    Double discountOnOrder();
}

Note: DiscountPurchaseOrder is an abstract interface that defines a general prototype. Access modifiers are not specified for the method defined in an interface; it contains only the method signature.

The EmployeeOrder class implements the DiscountPurchaseOrder interface for Employee purchase orders.

// One implementation of the interface for Employees
public class EmployeeOrder implements DiscountPurchaseOrder {
    public Double discount() {
        return .08;  // For Employees discount should be 8%
    }
}

Another class CustomerOrder implements the DiscountPurchaseOrder interface for Customer purchase orders.

// Another implementation of the interface for customers 
public class CustomerOrder implements DiscountPurchaseOrder {
      public Double discount() {
        return .04;  // For customers discount should be 4%
      } 
}

Note: You can see that both the CustomerOrder and EmployeeOrder classes implement the DiscountPurchaseOrder interface, so the DiscountPurchaseOrder interface body must be defined in the Discount method of the class. All methods of an interface must be defined in the class that implements the interface.

CRS Info Solutions offers a comprehensive and dynamic Salesforce online course career building program for beginners, covering admin, developer, and LWC concepts. This course features immersive real-time projects, interactive hands-on learning, detailed daily notes, essential interview questions, thorough certification preparation, and strategic job prep guidance. Join their inspiring free demo to embark on an exciting Salesforce journey with expert mentorship and unlock your full potential in the Salesforce ecosystem. Enroll for a free demo today!

Next chapter is DML and previous chapter is Objects in Apex.

Comments are closed.