item 21 : 인터페이스는 구현하는 쪽을 생각해 설계하라
1. 기존 인터페이스에 디폴트 메서드 구현을 추가하는 것은 위험한 일이다.
1) Collection의 removeIf 메서드
// Collection.java
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}2) SynchronizedCollection과의 충돌
3) 디폴트 메서드의 위험 해결 방법
2. Default 메서드는 기존 구현체에 런타임 에러를 발생시킬 수 있다.
✨ 최종 정리
Last updated