I was reading a post on LinkedIn (which I will not link to, haha) about why we use interfaces. I’ve found that the concept of interfaces is not something that junior to mid-level developers easily understand. Here’s the answer:
Interfaces let you separate specification from implementation.
The problem with this answer is that it leads to the next question:
Why is it useful to separate specification from implementation?
This is the sticky point — if you don’t have a lot of experience writing code, you probably don’t know why, simply because you haven’t had enough real world experience to understand this concept. So here’s the rest:
When specification is separated from implementation, it allows the implementation to change. In terms of actual usage, it means that different implementations can be concretely instantiated, each implementing the same specification. Once instantiated, the user of the specification (another part of the program) works with the implementation through the interface rather than the concrete implementing type. This way, the user of the specification doesn’t and shouldn’t care about the underlying implementing type.