When in your code you should use interfaces (Java)?

0 like 0 dislike
4 views
Good day! I read that the use of interfaces in Java is important and is good practice. But I have no clear understanding of when in your code you should use them. I would be very happy to see examples with reasoning of why they are used.
by | 4 views

1 Answer

0 like 0 dislike
DevMan is right - for a paradigm ever!
In practice, any code (class or interface) requires time consuming to create and maintain. To accompany the primitive class is simpler than the primitive class and its interface.

Therefore, in practice, usually writing interfaces:
1. As a necessary part of patterns;
2. If "the interface is the contract of cooperation" to develop a library or component, here the interfaces are immutable part of the contract;
3. Good practice - all services to do with interfaces. By pushing Gramercy JAVA as an example: Spring and OSGi (Apache Felix);
4. For JUnit testireba. Example: when testing the business logic, real services are replaced by test stubs. Appropriately classy business logic needs to use the interfaces in real work Semenyuta real implementations, and test environment - the test plugs.
by

Related questions

0 like 0 dislike
7 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
32,718 users