Activity Forums Salesforce® Discussions Why do we use interface in Salesforce Apex Class?

  • saloni gupta

    Member
    July 18, 2017 at 1:24 pm

    hi parv,

    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.

    public class InterfaceClass
    {
        //Lets consider a Bank trnasaction interface which indeed used by 3 banks BankA, BankB and BankC
        public Interface bankTransactionInterface
        {
            double deposit();
            double withdrawal();
        }
    
        //We have to implement the two methods declared in the interface for BankA
        public class BankA implements bankTransactionInterface
        {
            public double deposit()
            {
                //process the deposit
                double depositedAmount = 250;
                return depositedAmount ;
            }
    
            public double withdrawal()
            {
                //process the withdrawal
                double withdrawalAmount = 350;
                return withdrawalAmount ;
            }
        }
    
        //We will take another class for BankB and declare it as virtual as it is parent of BankC which has different deposit porcess but same withdrawal process as BankB.
        //For this we have to declare the deposit method as virtual and use override keyword when overriding it for BankC like showed below
        public virtual class BankB implements bankTransactionInterface
        {
            public virtual double deposit()
            {
                //process the deposit
                double depositedAmount = 450;
                return depositedAmount ;
            }
    
            public double withdrawal()
            {
                //process the withdrawal
                double withdrawalAmount = 1000;
                return withdrawalAmount ;
            }
        }
    
        public class BankC extends BankB
        {
            public override double deposit()
            {
                //process the deposit
                double depositedAmount = 750;
                return depositedAmount ;
            }
        }
    }
  • shariq

    Member
    July 19, 2017 at 1:47 pm

    Hi parv,

    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.

    For more information you can click here

  • Avnish Yadav

    Member
    September 30, 2018 at 5:38 am

    Hello,

    Let see an scenario,

    For example, implementing the Schedulable interface guarantees that System.schedule knows how to run the code inside of the class, while implementing Comparable allows List.sort to know how to sort a collection of that object. This allows a given method to know how to interact with objects that may well be implemented after the original code was written. Without interfaces, you'd have to write a new copy of a given algorithm for every new data type.

    Thanks.

  • Parul

    Member
    September 30, 2018 at 2:04 pm

    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.

Log In to reply.

Popular Salesforce Blogs