- 最終更新
- 2026/08/17
3秒でわかる
実装を持たずメソッドの並びだけを決める型。呼ぶ側を具体的なクラスから切り離し、後から差し替えられる設計にするために使います。
30秒図解
もう少し詳しく
どういうものか
interface は
Java 8 以降は
なぜ必要か
呼ぶ側が
具体例
public interface Notifier {
void send(String message);
}
public class MailNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("mail " + message);
}
}
public class OrderService {
private final Notifier notifier;
public OrderService(Notifier notifier) {
this.notifier = notifier;
}
public void complete(String orderId) {
notifier.send("order done " + orderId);
}
}OrderService は
つまずきやすいところ
まず、
次に、
もう
似た用語との違い
| 比較 | interface | 抽象クラス |
|---|---|---|
| 状態 | 持てない | フィールドを持てる |
| 多重 | 複数実装できる | 継承は一つだけ |
| 使い分け | 役割や | 共通の |
覚え方
interface は

