# Io / Github / Biezhi / Java11 / Interfaces

## Overview

This module appears to demonstrate Java 9 interface private methods, with a small example interface that combines a traditional abstract method, default methods, and private helper methods. Its purpose is educational: it shows how interfaces can share internal implementation details without exposing those details as part of the public API.

The code is intentionally small, but it captures an important design idea for modern Java interfaces: reusable logic can live inside the interface itself, while callers and implementers only see the contract that matters.

## Key Classes and Interfaces

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

`Example` is the only source type in this package, and it is an interface rather than a concrete class. It demonstrates how an interface can define both API surface and internal behavior.

#### Purpose

The interface separates three concerns:

- **Required behavior** through `normalInterfaceMethod()`
- **Reusable public behavior** through default methods
- **Internal implementation detail** through private helper methods

The class-level comment explains the motivation: before Java 9, default methods in interfaces could not easily share code without duplicating logic or exposing helper methods as part of the public contract. This interface shows the Java 9+ solution.

#### Methods

- [`sayHello()`](java11-examples-master/src/main/java/io/github/biezhi/java11/interfaces/Example.java:21)
  - A `private static` helper that prints a greeting message.
  - It takes no parameters and returns nothing.
  - Because it is private, it is only usable inside the interface itself.
  - In this file it is not invoked by any other method, so it functions as a self-contained example of a private interface method.

- [`normalInterfaceMethod()`](java11-examples-master/src/main/java/io/github/biezhi/java11/interfaces/Example.java:26)
  - The single abstract method in the interface.
  - It takes no parameters and returns `void`.
  - Any implementing class must provide this behavior.
  - This is the actual contract that consumers of the interface are expected to fulfill.

- [`interfaceMethodWithDefault()`](java11-examples-master/src/main/java/io/github/biezhi/java11/interfaces/Example.java:29)
  - A default method that provides an implementation in the interface.
  - It takes no parameters and returns `void`.
  - Its implementation calls the private helper `init()`.
  - This keeps reusable logic in one place and avoids repeating the same setup code in multiple default methods.

- [`anotherDefaultMethod()`](java11-examples-master/src/main/java/io/github/biezhi/java11/interfaces/Example.java:34)
  - Another default method with the same setup behavior as `interfaceMethodWithDefault()`.
  - It also takes no parameters and returns `void`.
  - It calls the same private helper `init()`, which is the central point of reuse in this example.

- [`init()`](java11-examples-master/src/main/java/io/github/biezhi/java11/interfaces/Example.java:39)
  - A `private` helper method used by the default methods.
  - It takes no parameters and returns nothing.
  - It prints a message indicating setup work is happening.
  - This method is not part of the public API and is intentionally hidden from implementations and callers.

## How It Works

The flow in this module is straightforward:

1. A consumer implements `Example` and provides `normalInterfaceMethod()`.
2. The interface supplies two default methods that already contain behavior.
3. Both default methods reuse a shared private helper, `init()`, instead of duplicating the same logic.

The important design point is that the helper logic stays inside the interface. That means the interface can evolve its implementation without exposing extra methods to implementers.

### Relationship map

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

  ExampleInterface --> AbstractMethod
  ExampleInterface --> DefaultOne
  ExampleInterface --> DefaultTwo
  ExampleInterface --> PrivateInit
  ExampleInterface --> PrivateHello
  DefaultOne --> PrivateInit
  DefaultTwo --> PrivateInit
```

## Data Model

There is no data model in this module. The package contains only one interface and no DTOs, entities, or value objects.

## Dependencies and Integration

This package has no resolved package dependencies in the index.

At the source level, the interface only depends on the Java language features for:

- interface abstract methods
- default methods
- private interface methods
- private static interface methods
- `System.out.println` for simple console output

Because the module is isolated, it appears to be used as a focused language example rather than as a reusable application component.

## Notes for Developers

- `normalInterfaceMethod()` is the only method implementers must provide.
- The private helper methods are intentionally hidden and should not be treated as public extension points.
- Both default methods currently do exactly the same setup work. If you add more behavior here, keep the shared work in `init()` so the example continues to demonstrate reuse instead of duplication.
- The `sayHello()` method is private static, but in this file it is not referenced. If you want the example to show actual use of the static helper, you would need to call it from within the interface itself.
- The messages printed by the helper methods are illustrative output only; they are not part of a larger protocol or API contract.

## Summary

`io.github.biezhi.java11.interfaces` is a compact Java 11 example package that demonstrates how interfaces can combine abstract methods, default methods, and private helpers. Its main lesson is that shared logic can stay inside the interface without becoming public API, which makes interface implementations cleaner and more maintainable.
