64
What are the main reasons (in practice) that lead developers to apply the practice of developing for interface and not for implementation?
64
What are the main reasons (in practice) that lead developers to apply the practice of developing for interface and not for implementation?
54
Because interfaces are only contracts of what the object has or is able to do. So you can use any object as long as the contract is guaranteed.
Having only the contract you can get better:
With the interface is one of the possible ways to reduce the coupling.
It helps in encapsulation and abstraction which is more than having some private members. Prioritizing the use of the interface the code clearly only says what it needs there and the languages usually prevent access to other members not present in the declared interface even if you know that these members exist in the object.
In some cases using interface is the same as using abstract classes.
There’s an answer which shows in practice how important it is to be as generic as possible in the type of object you want and as specific as possible in what you want to do with this object, when you use the interface, and it is set correctly, you are saying that you will only do something there that the interface allows, even if the object as a whole allows more.
This facilitates several design standards, especially the dependency injection and control inversion.
Browser other questions tagged oop software-engineering interface programming-principles
You are not signed in. Login or sign up in order to post.