Why should the interface for a Java class be preferred?

Using interfaces over concrete types is the key for good encapsulation and for loose coupling your code.

It’s even a good idea to follow this practice when writing your own APIs. If you do, you’ll find later that it’s easier to add unit tests to your code (using Mocking techniques), and to change the underlying implementation if needed in the future.

Here’s a good article on the subject.

Hope it helps!

Leave a Comment