Interfaces in Salesforce Apex and How to Use it

In this Salesforce tutorial, we will learn about interfaces in Salesforce Apex. I will explain what an interface is, and then, using an example, we will see how to implement an interface in Salesforce Apex.

Additionally, I will explain the term multiple inheritance in Salesforce Apex using interfaces and also explain Apex code so that we can achieve multiple inheritance in Apex.

What is an Interface in Salesforce Apex?

In Salesforce Apex, the interface is similar to the Apex class, as it contains methods without a body. This means that whenever we create a class and method, we do not implement the logic in the declared methods; instead, they are defined as an interface.

The interface decides the behaviour of the child class. To implement the logic of empty methods, we need to extend the interface class. In the inherited class, we can add logic for the methods.

Interface can be used to add an abstraction to code. As a result, we can implement methods whenever we want. 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.

Syntax of Interface in Salesforce Apex

Create an Apex class. When creating an interface, ensure that you add the ‘Interface’ keyword before the class name.

public interface Baseclass {
      void method1();
      void method2();       
}

Then, create another class, and in this class, we can implement the empty methods that we declared in the interface class.

To extend the interface class, we need to add the ‘implements’ keyword before the interface class name.

public class newClass implements Baseclass {
      public void method1() {
            //logic;
    }
     public void method2() {
           //logic;
    }
}

Implement Interface in Salesforce Apex

In the Apex code below, I will explain how to create an interface and methods without any logic in the Salesforce Apex class and implement this interface and these methods in other classes.

Here, we created an interface that contains three empty methods: getBalance(), deposit(), and withdraw(). Now, we need to implement these methods in another class, and that class should also implement this interface.

public interface bankProcess {
    void getBalance();
    void deposit (Decimal amount);
    void withdraw (Decimal amount);
}

Now, we have created another class that implements the BankProcess interface. Then, in this implemented class, we again defined the methods that we declared in the interface class and left empty.

However, we implemented those methods and entered logic for each function of those methods.

public class bankAccount implements bankProcess {
    private decimal balance = 0;
    public bankAccount (decimal initialBal) {
        this.balance = initialBal;
    }
    public void getBalance() {
        System.debug ('Current balance: ' + balance);
    }
    public void deposit (Decimal amount) {
        if (amount > 0) {
            balance = balance + amount;
            System.debug (' Deposited Amount : ' + amount);
            System.debug (' New Total balance : ' + balance);
        } else {
            System.debug ('Invalid deposit amount');
        }
    }
    public void withdraw (Decimal amount) {
        if (amount > 0 && amount <= balance) {
            balance = balance - amount;
            System.debug (' Withdrawn Amount: ' + amount);
            System.debug (' New Total balance : ' + balance);
        } else {
            System.debug ('Invalid withdrawal amount or insufficient funds');
        }
    }
}

As you execute the implemented class and call the methods, you will see the output according to the logic or operation methods performed.

Interfaces in Salesforce Apex and How to Use it

Multiple Inheritance in Salesforce Apex

In Salesforce Apex, we can implement single inheritance, but multiple inheritance is not supported directly, meaning a class cannot inherit from more than one class. To implement various inheritance, we need to utilize interfaces.

Syntax of Multiple Inheritance in Salesforce Apex

We created two separate interfaces with empty methods. Then, we need to create a new class in which we can implement the interfaces and methods we have created. In this way, we can implement multiple inheritance in Salesforce Apex.

public interface FirstInterface {
    void FirstInterfaceMethod();
}
public interface SecondInterface {
    void SecondInterfaceMethod();
}
public class newClass implements FirstInterface, SecondInterface {
    public void FirstInterfaceMethod() {
        System.debug('Implementing method from First Interface');
       }
    public void SecondInterfaceMethod() {
        System.debug('Implementing method from Second Interface');
      }
}

Execute Multiple Inheritance or Interface

Create a class object in which you implemented the interface and call the methods using the object instance.

newClass Obj_instance = newClass();
Obj_instance.FirstInterfaceMethod();
Obj_instance.SecondInterfaceMethod();

Multiple Inheritance Using Interface in Salesforce Apex

In the steps below, I have explained how to create multiple inheritance by implementing an interface in Salesforce Apex.

For example, we have two different classes, and we want to display or access the information of both classes in a single class. To achieve this, we need to implement multiple inheritance, which can be accomplished using interfaces.

Here, I created an interface for petrol cars and declared an empty method.

public interface PetrolCars {
    void PetrolCarsDetails();  
}

Similarly, we created another interface for electric cars, and again, an empty method was defined. Now, we will implement these methods in the new class, which also implements these interfaces.

public interface ElectricCars {
    void ElectricCarsDetails();
}

Now, create the class that implements both the interfaces and methods. Here, we can implement the methods. That means we can write the logic of whatever operation we want to perform using these methods.

public class AllCars implements PetrolCars, ElectricCars {
    
    public void PetrolCarsDetails() {
        String make = 'TATA';
        String Mmodel = 'Altroz';
        String ModelType = 'Petrol';
        Date manufactured = Date.newInstance(2020, 10, 09);
        String color = 'While';
        Decimal price = 850000;
        system.debug ('Car Brand : ' +make);
        system.debug ('Model Name : ' +Mmodel);
        system.debug ('Fule Type : ' +ModelType);
        system.debug ('Manufactured Date : ' +manufactured);
        system.debug ('Color : ' +color );
        system.debug ('Price : ' +price );
    }

    public void ElectricCarsDetails() {
        String make = 'Tesla';
        String ModelType = 'Electric';
        Date manufactured = Date.newInstance(2022, 05, 20);
        String color = 'Blue';
        Decimal price = 4050000;
        system.debug ('Car Brand : ' +make);
        system.debug ('Model  : ' +ModelType);
        system.debug ('Manufactured Date : ' +manufactured);
        system.debug ('Color : ' +color );
        system.debug ('Price : ' +price );
    }
}

To execute the code, we need to create an object of the class that implements the interface and method, and then call the method we want to execute.

Multiple Inheritance using Interface in Salesforce Apex

In this way, we can implement multiple inheritance or interfaces in Salesforce Apex.

Conclusion

I hope you have got an idea about the interface and multiple inheritance in Salesforce Apex. In this tutorial, we have seen what an interface is and its syntax. Then, I explained how to achieve multiple inheritance in Salesforce Apex using an interface with syntax and examples.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.