0

Using the Spring Framework, it is customary to create a service to create an interface that defines the main methods of the service.

interface MultiplicationService {
    long execute(int a, int b);
}

class MultiplicationServiceImpl implements MultiplicationService {
    // implementation
}

What is the point of creating an interface?

Arkady
  • 39
  • 4

1 Answers1

1

The point of creating an interface for any class is to separate the API from the implementation. The objective is to make it possible to change the implementation without affecting clients.

You can find plenty of examples in the JDK itself:

  1. Collections API has several implementations for the List interface.
  2. JDBC is all interfaces, allowing you to change relational database vendors without having to rewrite your code.

Spring can easily generate proxy byte code for interfaces as well - useful for AOP, transaction managers, etc.

duffymo
  • 305,152
  • 44
  • 369
  • 561