In simple terms, an interface is like a contract or blueprint that says,
“If you want to be part of this system, you must have these methods.”
It doesn’t contain any actual code, only the rules — the names of functions or properties that a class must have.
An interface is a contract that defines what methods or properties a class must have,
but leaves the implementation details to the class itself.
Example (TypeScript or Java-style syntax):
interface PaymentProcessor {
processPayment(amount: number): void;
}
Now, any class that implements this interface must define that method:
class CreditCardPayment implements PaymentProcessor {
processPayment(amount: number) {
console.log(`Processing $${amount} via credit card`);
}
}
class PayPalPayment implements PaymentProcessor {
processPayment(amount: number) {
console.log(`Processing $${amount} via PayPal`);
}
}
Top comments (0)