Interface định nghĩa contract — danh sách method mà implementation phải cung cấp. Tách "làm được gì" khỏi "làm thế nào". Một class implements nhiều interface.
Lịch sử tiến hoá:
| Phiên bản | Thêm gì |
|---|---|
| Java ≤ 7 | Chỉ public abstract method + public static final constant |
| Java 8 | default method (có implementation), static method |
| Java 9 | private method (helper cho default) |
public interface PaymentGateway {
String VERSION = "v2"; // implicit public static final
void charge(BigDecimal amount, String currency); // abstract
default void chargeUSD(BigDecimal amount) { // default — Java 8+
charge(amount, "USD");
}
static PaymentGateway noOp() { return (a, c) -> {}; } // static — Java 8+
}Vì sao Java 8 thêm default? Để evolve interface mà không break implementation cũ — vd stream() được thêm vào Collection qua default method, không thì mọi Collection từ thư viện ngoài đều fail compile.
Quy tắc:
- Field ngầm public static final; method ngầm public abstract.
- Interface không có constructor, không có state (chỉ hằng số).
- Implement nhiều interface có default trùng → phải override tường minh.
Khi dùng interface (vs abstract class):
- Capability/contract cho nhiều class không liên quan (Comparable, Iterable).
- Public API thư viện, DI/strategy pattern.
- Cần đa kế thừa contract.
Functional interface (đúng 1 abstract method) implement được bằng lambda — nền tảng của Stream/Optional API.
Java 17+ sealed interface: giới hạn class nào được implement, kết hợp pattern matching cho exhaustive switch.
An interface defines a contract — a list of methods implementations must provide. It separates "what can be done" from "how". A class can implements many interfaces.
Evolution timeline:
| Version | Added |
|---|---|
| Java ≤ 7 | Only public abstract methods + public static final constants |
| Java 8 | default methods (with implementation), static methods |
| Java 9 | private methods (helpers for defaults) |
public interface PaymentGateway {
String VERSION = "v2"; // implicit public static final
void charge(BigDecimal amount, String currency); // abstract
default void chargeUSD(BigDecimal amount) { // default — Java 8+
charge(amount, "USD");
}
static PaymentGateway noOp() { return (a, c) -> {}; } // static — Java 8+
}Why Java 8 added defaults? To evolve interfaces without breaking existing implementations — e.g. stream() was added to Collection as a default method, otherwise every external Collection implementation would fail to compile.
Rules:
- Fields are implicitly public static final; methods implicitly public abstract.
- Interfaces have no constructors, no state (constants only).
- Implementing multiple interfaces with conflicting defaults → must override explicitly.
When to use an interface (vs abstract class):
- Capability/contract across unrelated classes (Comparable, Iterable).
- Public API of libraries, DI/strategy patterns.
- Multiple inheritance of contracts.
Functional interface (exactly one abstract method) can be implemented with a lambda — the foundation of Stream/Optional APIs.
Java 17+ sealed interfaces: restrict which classes can implement, combined with pattern matching for exhaustive switches.