An interface in programming is a contract that lists what a piece of code must be able to do, without saying how it does it. It names a set of actions, and any code that claims to follow the interface promises to provide all of them. The interface itself does no work; it is the agreement, and other code supplies the actual behavior. This separation of the what from the how is what makes interfaces so useful: you can swap one implementation for another as long as both honor the same contract. Here is what an interface is, how it works, and why it keeps code flexible.
How an interface works
An interface declares a list of capabilities. A payment interface might say anything that follows me must be able to charge a card and issue a refund, without specifying which payment provider or how. Different classes can then implement that interface, each filling in the details its own way.
The power comes from the promise. Once code is written to depend on the interface rather than on any specific implementation, you can substitute one implementation for another without changing the code that uses it. As long as the new piece honors the contract, everything keeps working. This is the same thinking behind clean, modular design.
If the related building block of classes is fuzzy, our explainer on what an object in programming is gives helpful grounding, since interfaces describe what objects can do.
What an interface is not
A common stumbling block is the word itself, which has two unrelated meanings in tech.
| Meaning |
What it refers to |
| Code interface |
A contract listing actions a class must provide |
| User interface |
The buttons and screens a person interacts with |
| Hardware interface |
A physical connection point, like a port |
This guide is about the code interface, the contract. It also is not a working thing you can run on its own; it has no behavior, only the list of promises that real code must keep. Mixing these meanings up is one of the most common early confusions, so it is worth being clear about which one a tutorial means.
Why interfaces keep code flexible
- Swappable parts. Code that depends on an interface does not care which implementation it gets, so you can replace one with another freely.
- Easier testing. You can substitute a simple stand-in that honors the same contract, making tests faster and more reliable.
- Clearer agreements between teams. One team can build against an interface while another finishes the implementation behind it.
- Reduced coupling. Parts of the system lean on the promise, not the internals, so a change in one place ripples less.
The thread tying all of this together is decoupling: separating what you need from who provides it. That is the core idea our guide to what encapsulation is explores from another angle.
Common mistakes
- Confusing it with a user interface. A code interface is a contract; a user interface is what a person sees and clicks.
- Putting behavior in the interface. The interface lists actions; the implementation supplies the actual work.
- Making interfaces too large. A bloated contract forces every implementer to provide things they may not need.
- Skipping interfaces entirely. Coding directly against one specific implementation makes future swaps painful.
FAQ
What is the difference between an interface and a class?
A class is working code with both data and behavior. An interface is only a contract that lists what actions must exist, and a class implements it by providing those actions.
Does an interface contain code that runs?
Traditionally no. An interface lists the required actions but leaves the actual behavior to the classes that implement it, though some languages allow limited default behavior.
Is a code interface the same as a user interface?
No. They share the word but are unrelated. A code interface is a programming contract; a user interface is the visual part a person interacts with.
Why would I use an interface?
To keep parts of your program swappable and loosely connected, which makes the code easier to test, extend, and maintain over time.
Where to go next
Build the foundation with What Is an Object in Programming in 2026, see how hiding details helps in What Is Encapsulation in 2026, and explore reuse through What Is Inheritance in 2026.