# Io / Github / Biezhi / Java11 / Interfaces

## Overview

This module appears to be a focused Java 11 interface example that demonstrates how interfaces can combine abstract methods, default methods, and private helper methods. The code is primarily educational: it shows why Java 9 added private interface methods and how they help avoid repeating shared logic inside multiple default methods.

The module is intentionally small and centered on a single type, `Example`, which acts as both the contract and the demonstration. Its main value is in showing the design boundary between public interface API and internal interface implementation details.

## Key Classes and Interfaces

### [Example](src/main/java/io/github/biezhi/java11/interfaces/Example.java:19)

`Example` is the only source type in this package and is declared as an interface. It demonstrates three interface features:

- a private static helper method used only inside the interface,
- one abstract method that implementers must provide,
- two default methods that share behavior through a private instance helper.

The code comments indicate that this is meant to illustrate the problem Java 8 interfaces had: once default methods were introduced, repeated helper code in multiple defaults either had to be duplicated or exposed as part of the public API. Java 9's private interface methods solve that by allowing reuse without leaking implementation details.

#### Methods

- `sayHello()` — [lines 21–23](src/main/java/io/github/biezhi/java11/interfaces/Example.java:21)
  - Declared as `private static void`.
  - Prints a greeting message to standard output.
  - It is a helper method internal to the interface and is not part of the public contract.
  - In the current source, it is not called by the other methods, so it reads as an isolated example of a private static interface method.

- `normalInterfaceMethod()` — [line 26](src/main/java/io/github/biezhi/java11/interfaces/Example.java:26)
  - Declared as an abstract method with no body.
  - Returns `void` and takes no parameters.
  - Implementing classes must supply the behavior.
  - This represents the traditional interface contract surface.

- `interfaceMethodWithDefault()` — [lines 29–31](src/main/java/io/github/biezhi/java11/interfaces/Example.java:29)
  - Declared as a `default void` method.
  - Takes no parameters and returns nothing.
  - Its implementation delegates to `init()`, which keeps the default method concise.
  - This shows how shared setup logic can be centralized inside the interface.

- `anotherDefaultMethod()` — [lines 34–36](src/main/java/io/github/biezhi/java11/interfaces/Example.java:34)
  - Also a `default void` method.
  - Takes no parameters and returns nothing.
  - Like `interfaceMethodWithDefault()`, it delegates to `init()`.
  - The two default methods demonstrate reuse of the same private helper from multiple public/default entry points.

- `init()` — [lines 39–41](src/main/java/io/github/biezhi/java11/interfaces/Example.java:39)
  - Declared as a `private void` instance method.
  - Prints a message to standard output.
  - This is the shared implementation detail used by the default methods.
  - The comment in the source makes the intent explicit: it is not part of the public API.

## How It Works

The package contains a single interface, so the execution flow is straightforward:

1. An implementing class must provide `normalInterfaceMethod()`.
2. Callers can invoke either default method without the implementation having to duplicate shared setup logic.
3. Both default methods route to the same private helper, `init()`.
4. `init()` performs the shared side effect by printing a message.

The structure is a simple example of interface-level code reuse:

- abstract method for required behavior,
- default methods for optional or shared behavior,
- private helper for internal reuse.

This keeps the public interface smaller and avoids exposing support methods that are only useful to the interface itself.

### Mermaid: interface structure

```mermaid
flowchart TD
    ExampleInterface["Example interface"] --> AbstractMethod["normalInterfaceMethod()"]
    ExampleInterface --> DefaultOne["interfaceMethodWithDefault()"]
    ExampleInterface --> DefaultTwo["anotherDefaultMethod()"]
    DefaultOne --> PrivateInit["init()"]
    DefaultTwo --> PrivateInit["init()"]
    ExampleInterface --> PrivateStatic["sayHello()"]
```

## Data Model

There is no data model in the usual sense. The module defines only behavior on an interface, with no fields, DTOs, or entity classes. The main “shape” here is the method contract of the interface itself.

## Dependencies and Integration

The module has no resolved package dependencies in the index, and the source file does not import any external libraries. Its only visible integration points are:

- standard output via `System.out.println(...)`,
- classes elsewhere in the codebase that may implement `Example`.

Because the module is self-contained, it serves more as a language feature sample than a production integration point.

## Notes for Developers

- `Example` is best read as a teaching example of Java 9+ interface capabilities, not as a reusable business service.
- `init()` is the key design idea: it removes duplication from default methods without turning the helper into a public API method.
- `sayHello()` demonstrates private static methods in interfaces, but the current implementation does not call it from the other methods.
- If you extend this interface in real code, remember that `normalInterfaceMethod()` must be implemented by every concrete class.
- The printed messages are side effects only; there is no return value or state management.
- The comments suggest a distinction between Java 8 and Java 9 behavior, so this module is likely intended to help readers understand the evolution of interface design in modern Java.
