Developer Dictionary
Interface
Definition
A shared boundary across which two or more separate components of a computer system exchange information.
Deep Dive
In object-oriented programming, an interface serves as a blueprint or a contract that defines a set of methods that a class must implement. Unlike a class, an interface does not contain any implementation details for these methods; it only declares their signatures (name, parameters, and return type). Any class that "implements" an interface promises to provide concrete implementations for all the methods defined within that interface, thereby adhering to a specific behavioral contract.
Examples & Use Cases
- 1A `Sortable` interface defining a `sort()` method. Different classes like `BubbleSortAlgorithm`, `QuickSortAlgorithm`, or `MergeSortAlgorithm` can all implement `Sortable`, each providing its unique sorting logic, yet all can be used interchangeably by a component expecting a `Sortable` object.
- 2A `PaymentGateway` interface with methods like `processPayment()` and `refund()`. Different payment providers (e.g., `StripeGateway`, `PayPalGateway`) can implement this interface, allowing an e-commerce application to switch between providers with minimal code changes.
Related Terms
Abstract ClassPolymorphismAPIContract