🧭Interface (Programming)
Also known as Interface, Protocol, Contract
A list of methods a class promises to have. It holds no code inside.
Think of it like
Think of it like the shape of a plug. Any lamp with that plug fits the socket.
Example
Jack's drawing app asks for any Shape that has an area method. Circle and Square both fill it in, so either one works.
Why it matters
Interfaces let parts of a program depend on promises, not on one exact class. That makes swapping parts easy.
Definition
Example code
typescript
interface Shape {
area(): number;
}Common mistake
Key takeaways
- 1A promise. These methods will exist.
- 2No code inside. Each class fills it in.
- 3Easy to swap. Any match will do.