1

At the beginning I know there are couple of similar subject already but decided to create my own as it's a bit more that others.

Think I know how to use interfaces (or not) - how it has to be declared, how class implements it and how to assign object of class which implements interface to this interface.

My main problem is I cannot enforce myself to use them somehow. I am not sure if I really understand when interface has to be used. When I am developing application I noticed that I am not using them or I use them but just simply to declare interface and consume by class as a contract but that's it - I just don't see usage of them later. Those which I much use is inheritance but not interfaces.

So out of what I just wrote the question is more like how to know that in this particular moment I should use them or better in what moment they could be used. How do you do it in your daily business work? Real world examples are welcomed.

Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47

1 Answers1

0

I agree with you when you don't have multiple implementations, then it is un-necessary to design the interface and then implement it. It will unnecessarily add a new file to code base. But we find in general practice people create an interface first then implement it. It could be because of 3 main reasons :

For Future : They can see multiple implementation in future, so to make the future implementation secure, lets define the contract during the first implementation.

For Abstraction : Interface will give a small and clear definition of your implementation. You can read the interface and get a quick view/understanding.

For Management : If you are busy with the implementing a component and others have a dependency on your component. So lets quickly design the interface and distribute to them so that they can continue on their part.

Manmay
  • 835
  • 7
  • 11