# Io / Github / Biezhi / Java11 / Interfaces

## Overview

This module appears to be a focused Java 11 interface example that demonstrates how interfaces can contain both default behavior and private helper methods. It exists to show how Java 9+ changed interface design: instead of duplicating shared logic across default methods, the interface can hide that logic behind private methods while keeping the public API small.

The code is intentionally minimal, but it captures an important language pattern for engineers working with interface evolution and API design. The key idea is that the interface exposes a normal abstract contract plus two default methods, both of which delegate to a private instance helper.

## Key Classes and Interfaces

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

`Example` is the only type in this package. It is an interface, not a concrete implementation, and its purpose is to illustrate interface members that became available in later Java versions.

#### Purpose and design role

The interface demonstrates three categories of members:

- an abstract method that implementers must provide,
- default methods that supply reusable behavior,
- a private helper method used only inside the interface itself.

It also contains a private static helper named `sayHello()`, showing that interfaces can hold internal utility logic that is not part of the public contract.

#### Methods

- **`void normalInterfaceMethod()`**
  - This is the abstract contract method.
  - It takes no parameters and returns nothing.
  - Implementations must supply their own behavior.
  - Because it is abstract, it represents the public “must implement” part of the interface.

- **`default void interfaceMethodWithDefault()`**
  - This default method provides a reusable implementation.
  - It takes no parameters and returns nothing.
  - Its entire body calls `init()`.
  - The method is a template for shared interface behavior that can be inherited by implementers.

- **`default void anotherDefaultMethod()`**
  - This is a second default method with the same delegation pattern.
  - It also takes no parameters and returns nothing.
  - It calls `init()` so the common behavior stays centralized.
  - The code comment describes it as another “snake skin” default method, which suggests it exists mainly to show reuse across multiple defaults.

- **`private void init()`**
  - This is the shared helper used by both default methods.
  - It takes no parameters and returns nothing.
  - It prints `正在给大佬准备洗脚水...` to standard output.
  - The method is private, so it is not part of the interface API visible to implementers.
  - This is the main example of code reuse inside the interface.

- **`private static void sayHello()`**
  - This is a private static helper inside the interface.
  - It takes no parameters and returns nothing.
  - It prints `你已经是大佬了，快和萌新打个招呼！` to standard output.
  - It is never called by the other methods in this file, so it appears to be an additional demonstration of interface-private static methods rather than active runtime behavior.

## How It Works

The execution model is straightforward:

1. A class that implements `Example` must provide `normalInterfaceMethod()`.
2. If a caller invokes `interfaceMethodWithDefault()` on an implementation, the default method runs from the interface.
3. If a caller invokes `anotherDefaultMethod()`, the same shared helper is reused.
4. Both default methods call the private `init()` method, which centralizes the printed message.

This structure keeps the repeated logic out of the public default methods and avoids exposing the helper as part of the interface contract.

## Mermaid: Interface structure

```mermaid
flowchart TD
    ExampleInterface["Example interface"]
    NormalMethod["normalInterfaceMethod()"]
    DefaultOne["interfaceMethodWithDefault()"]
    DefaultTwo["anotherDefaultMethod()"]
    PrivateInit["init() private helper"]
    PrivateSayHello["sayHello() private static helper"]
    ExampleInterface --> NormalMethod
    ExampleInterface --> DefaultOne
    ExampleInterface --> DefaultTwo
    ExampleInterface --> PrivateInit
    ExampleInterface --> PrivateSayHello
    DefaultOne --> PrivateInit
    DefaultTwo --> PrivateInit
```

## Data Model

There is no domain data model in this module. The only meaningful structure is the interface contract itself, which consists of behavior rather than stored state.

## Dependencies and Integration

This module does not depend on any other package in the documented codebase, and no package dependencies were resolved in the index. The only integration point is with whatever concrete classes implement the `Example` interface.

The module also integrates with the Java language runtime itself by using interface features introduced in Java 8 and Java 9:

- default methods
- private interface methods
- private static interface methods

## Notes for Developers

- The private helper `init()` is the central reuse point. If you add more default methods, prefer delegating common work to this helper instead of duplicating logic.
- Because `sayHello()` is private and static, it cannot be used by implementers or callers. It serves only as an internal example.
- `normalInterfaceMethod()` is the only required contract method. Any implementation must define it.
- This interface is best treated as a language-feature example rather than a production service abstraction.
- If you extend this interface, keep in mind that the default methods currently have side effects only through console output.

## Reference

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