Conclusion
By providing a clear separation between a method declaration and implementation, interfaces enable the addition of an abstraction layer to code. As a result, it becomes possible to have different implementations of a method based on specific conditions.
Table of Contents
Recap to previous
Session
In the previous session, we learned how inheritance works in Salesforce.
Introduction to
Interface
By providing a clear separation between a method declaration and implementation, interfaces enable the addition of an abstraction layer to code. As a result, it becomes possible to have different implementations of a method based on specific conditions.
- Within a class that is the interface, we can only define methods.
- The interface cannot provide the implementations for its method.
- The visibility of the methods will be controlled by the class that is implementing the interface.
- In the interface, we cannot define any properties or variables except the methods.
- Multiple interfaces can be implemented by a Single class.
- A single interface can be implemented by Multiple Classes.
- An Interface can extend another interface.
- An interface can not be instantiated means an object can not be created
Interface Declaration
Examples
As an example, we can take from the Banks where every bank has its own interest for a different type of loans. Here is how the Apex Class will look like that is implementing the interface
Hands-On –
- Implement discounts using the IPurchaseOrder Interface.
- Implement Payment processing with IPaymentProcessor
Interface
The class that implements the interface. To implement the interface, we need to use implements keywords
Hands-On Code
Watch Complete Video
Resources
- https://www.jamessimone.net/blog/joys-of-apex/abstract-classes-and-interfaces/
- https://www.salesforcetutorial.com/static-final-this-super-keywords-in-apex/
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm
- https://www.javatpoint.com/method-overloading-in-java
- https://www.educative.io/answers/overloading-vs-overriding
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_extending.htm
- https://atrium.ai/resources/what-is-an-interface-in-apex-salesforce-and-how-to-use-it/
- https://beyondthecloud.dev/blog/abstract-virtual-interface-in-apex
- https://salesforce.stackexchange.com/questions/209510/whats-the-difference-between-abstract-class-and-virtual-class
