Activity › Forums › Salesforce® Discussions › Why do we use interface in Salesforce Apex Class?
Tagged: Comparable Interface, Help & Training, Salesforce Apex, Salesforce Apex Class, Schedulable Interface
-
Why do we use interface in Salesforce Apex Class?
Posted by Parv Shekhar Singh on July 18, 2017 at 12:47 PMWhen we should use it ?
Parul replied 7 years, 6 months ago 5 Members · 4 Replies -
4 Replies
-
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 ; } } } -
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
-
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.
-
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
4 Features That Make Salesforce Pardot Great for B2B Marketing
B2B marketing needs the right measurements to scale the marketing processes for better results. You can only take it to a small scale if you…
Learn All About Process Builder in Salesforce and Its Features
Would you like to make a record for any item from a work process? Would you like to call your Apex class from a work…
A Nonprofit's Success with Salesforce
Case Study: Nonprofit Organization Leverages Salesforce for Growth Organization: The Community Foundation Challenge: The Community Foundation struggled to manage donor relationships, track donations effectively, and…