In programming, an interface is a collection of abstract methods and constants that can be implemented by a class. Here are some uses of interfaces in programming:
Abstraction: Interfaces allow you to define a contract or a set of rules that must be followed by any class that implements the interface. This helps to separate the implementation details from the interface, making the code more modular and easier to maintain.
Polymorphism: Interfaces allow you to achieve polymorphism in your code. Since any class that implements the interface must provide an implementation for all of the methods in the interface, you can create objects of different classes that implement the same interface and treat them as if they were the same type.
Multiple Inheritance: In some programming languages, a class can only inherit from one parent class. However, a class can implement multiple interfaces, allowing it to inherit from multiple sources. This makes it easier to create complex class hierarchies without having to rely on multiple inheritance.
In this example, we define an interface called Interest that has one method called calculatePaymentAmount(). Any class that implements the Interest interface must provide an implementation for the calculatePaymentAmount() method.
Here's an example of a class that implements the Interest interface:
In this example, the HomeLoanPayment class implements the Interest interface by providing an implementation for the calculatePaymentAmount() method. This method calculates the payment amount for a Home Loan.
By using an interface in this way, we can define a common set of methods that must be implemented by any class that implements the interface. This allows us to write more flexible and reusable code.
Where do we use Interfaces in Apex
Batch Apex: Developer needs to implement the Database.Batchable interface. Thats why we need to write three methods namely, Start, Execute and Finish.
Queueable Apex: In this scenario, developer needs to implement Queueable interface and implement the Execute method.
Let me know your thoughts in the comment section!!
No comments:
Post a Comment