• #21077
    Forcetalks

    Why do we use interface in Salesforce Apex Class?

    When we should use it ?

     

    #21078
     saloni gupta #526
    Forcetalks

    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 ;
    }
    }
    }

    #21134
    Salesforce | shariq shariq #1
    Forcetalks

    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

    #41483
    Forcetalks

    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.

    #41791
    Salesforce | Parul Parul #2
    Forcetalks

    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.

Viewing 5 posts - 1 through 5 (of 5 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?