Course contentsShow
← Java
Lesson 2502 of 2,87047. Java Platform Module System (JPMS)Free lesson

Creating a Service Interface Module

Defining a module that exports only the service interface, establishing the contract for providers.

Creating a Service Interface Module

You'll learn how to define a module that exports only a service interface, establishing a clear contract between service consumers and providers.

The Core Idea

A service interface module is a dedicated module that contains only the interface (or abstract class) that defines a service contract. This module exports that interface to any module that needs it, but contains no implementation. Think of it as publishing an API specification: anyone can read it (consume the service) or implement it (provide the service), but the specification itself remains clean and implementation-free.

This separation creates a strict boundary: consumers depend only on the interface, not on any particular implementation. Providers can come and go without affecting consumers.

Structure of a Service Interface Module

Your service interface module typically contains:

  1. A single package with the service interface
  2. A module descriptor that exports that package
  3. No implementation classes
// file: com/example/logging/Logger.java
package com.example.logging;

public interface Logger {
    void log(String message);
    void error(String message);
}
// file: module-info.java
module com.example.logging.api {
    exports com.example.logging;
}

Notice there's no provides or uses here—this module simply makes the interface available. Consumer modules will add uses, and provider modules will add provides...with.

Why This Matters

By isolating the interface in its own module, you create maximum flexibility. Multiple providers can implement the service without any circular dependencies, and consumers remain decoupled from implementation details.

Key Takeaway:
A service interface module exports only the contract (interface), creating a clean separation between service definition, consumption, and implementation in the module system.