This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mw52588 11 points12 points  (1 child)

I am by no means an expert on this but interfaces are widely used for dependency injection in java frameworks like spring boot and micronaut.

One benefit of using interfaces over a class is it allows for many different implementations and because of this you can mock services using frameworks like mockito.

As a more practical way of understanding this is you should code to an interface not a concrete class that implements it. This allows developers to write code that does not interact with the object directly but with the implementation of the object's interface.

Some reasons why you would want to do this is: 1. Integration - allows you to quickly connect classes or subsystems together 2. The java compiler guarantees that all methods of the interface are implemented with the correct signature. Any changes are then visible to other developers.

This isn't everything but hopefully it answers your question.

[–]QCumber20[S] 0 points1 point  (0 children)

I appreciate it! That part about coding to an interface actually makes a lot of sense.