Java Interfaces Methodology: Should every class implement an interface?

You don’t need to create an interface if you are not going to use it.

Typically you need an interface when:

  • Your program will provide several implementations for your component. For example, a default implementation which is part of your code, and a mock implementation which is used in a JUnit test. Some tools automate creating a mock implementation, like for instance EasyMock.
  • You want to use dependency injection for this class, with a framework such as Spring or the JBoss Micro-Container. In this case it is a good idea to specify the dependencies from one class with other classes using an interface.

Leave a Comment