# Io / Github / Biezhi / Java11 / Interfaces

## Overview

This module demonstrates **private methods in Java interfaces**, a feature introduced in **Java 9** (not Java 11, despite the module path). The `io.github.biezhi.java11.interfaces` package is part of a larger tutorial series covering new language features across JDK releases. Specifically, this module shows how Java 9 solved the problem of code reuse between default methods in interfaces — a gap that existed since default methods were added in Java 8.

## Background: The Problem This Module Solves

Java 8 introduced default methods to interfaces, allowing interfaces to contain implementations rather than just method signatures. This was a powerful step toward functional-style programming in Java. However, it introduced a new problem:

When multiple default methods in the same interface share nearly identical logic, the developer's instinct is to extract that shared logic into a private helper method. But in Java 8, **interface methods could not be private** — every method in an interface was implicitly `public`. Creating a helper as a `default` method would expose it as part of the public API, which is undesirable for internal utility code.

Java 9 solved this by allowing `private` methods in interfaces — both instance and static variants. This module demonstrates that feature.

## Key Classes and Interfaces

### `Example` (interface)

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

This is the sole and primary type in this module. It is a `public interface` named `Example` that demonstrates all four kinds of method declarations available in Java 9+ interfaces:

1. **Abstract methods** — the traditional interface method with no body
2. **Default methods** — methods with a body that implementing classes inherit
3. **Private instance methods** — helper methods callable only from within the interface
4. **Private static methods** — utility methods callable only from within the interface

#### Method Details

##### `sayHello()` — `private static void`

```java
private static void sayHello() {
    System.out.println("你已经是大佬了，快和萌新打个招呼！");
}
```

- **Access modifier:** `private static` — a Java 9 addition
- **Purpose:** Demonstrates a private static utility method inside an interface. It can be called from other static or instance methods within the interface, but is not accessible to implementers.
- **Side effects:** Prints a message to `System.out`.
- **Note:** This method has no callers within the interface itself, so it serves purely as a demonstration of the language feature.

##### `normalInterfaceMethod()` — `void` (abstract)

```java
void normalInterfaceMethod();
```

- **Access modifier:** implicitly `public abstract`
- **Purpose:** Represents a traditional interface method that any class implementing `Example` must provide an implementation for.
- **Signature:** Takes no parameters, returns nothing.

##### `interfaceMethodWithDefault()` — `default void`

```java
default void interfaceMethodWithDefault() {
    init();
}
```

- **Access modifier:** `default` — inherited by implementers
- **Purpose:** Demonstrates a default method that delegates to a private helper method (`init()`). This is the core pattern the module illustrates: a default method calling a private method to share logic.
- **Side effects:** Prints a message via its call to `init()`.

##### `anotherDefaultMethod()` — `default void`

```java
default void anotherDefaultMethod() {
    init();
}
```

- **Access modifier:** `default` — inherited by implementers
- **Purpose:** A second default method that shares the same internal logic as `interfaceMethodWithDefault()` by calling the private `init()` method. This demonstrates the code reuse scenario that private interface methods enable.
- **Side effects:** Prints a message via its call to `init()`.

##### `init()` — `private void`

```java
private void init() {
    System.out.println("正在给大佬准备洗脚水...");
}
```

- **Access modifier:** `private` — a Java 9 addition, only visible within the interface
- **Purpose:** The shared internal helper that both default methods call. Without this private method, the two default methods would need to duplicate the `System.out.println()` call.
- **Side effects:** Prints a message to `System.out`.

## Relationships

```mermaid
flowchart LR
    A["Example (interface)"]
    A --> B["sayHello()
private static"]
    A --> C["normalInterfaceMethod()
abstract"]
    A --> D["interfaceMethodWithDefault()
default"]
    A --> E["anotherDefaultMethod()
default"]
    A --> F["init()
private"]
    D --> G["calls init()"]
    E --> G
```

- `interfaceMethodWithDefault()` and `anotherDefaultMethod()` are **default methods** that delegate to the private helper `init()`.
- `normalInterfaceMethod()` is an **abstract method** — the only one that implementers must override.
- `sayHello()` is a **private static** method — standalone and not referenced by other methods in the interface.
- `init()` is a **private instance** method — used exclusively as an internal helper by the default methods.

## How It Works

The flow through the interface methods illustrates the Java 9 private-method pattern:

1. A class implementing `Example` can call either `interfaceMethodWithDefault()` or `anotherDefaultMethod()` (since they have default implementations).
2. Whichever default method is called, it immediately delegates to `init()`, the private helper.
3. `init()` performs the actual work (printing a message) and returns.
4. The abstract method `normalInterfaceMethod()` has no default — any implementer must provide its own implementation.
5. `sayHello()` is a standalone private static utility, available only within the interface body.

This pattern ensures that if the shared logic needs to change, it only needs to be updated in one place: the `init()` method. Both default methods automatically benefit.

## Data Model

This module contains no data-bearing classes, DTOs, or entity types. The interface is purely behavioral, demonstrating method declarations and access modifiers in interfaces.

## Dependencies and Integration

- **No external dependencies.** The interface only uses `java.lang.System` (via `System.out.println`).
- **No cross-module relationships.** This module is a standalone demonstration — it does not import or reference any other module.
- **No package dependencies detected** in the module index.

## Notes for Developers

- **Java 9 feature in a Java 11 package path.** Despite being under `java11`, the private method feature this module demonstrates was introduced in Java 9. The parent project appears to group all JDK new-feature demos under a `java11` umbrella.
- **Single interface, four method kinds.** This module is intentionally minimal — one interface showing all four method declaration types. It serves as a reference example, not a production module.
- **`sayHello()` has no callers.** This private static method is demonstrated for completeness but is not invoked by any other method in the interface.
- **Implementers must implement `normalInterfaceMethod()`.** All other methods have default implementations, so the only obligation for a class implementing `Example` is to provide `normalInterfaceMethod()`.
