DEV Community

Khafido Ilzam
Khafido Ilzam

Posted on

What is an Interface?

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;
}
Enter fullscreen mode Exit fullscreen mode

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`);
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)