상속 (2) 썸네일형 리스트형 4-5) 상속을 고려해 설계하고 문서화하라. 그러지 않았다면 상속을 금지하라 상속을 고려한 문서화 public abstract class AbstractCollection implements Collection { ... /** * {@inheritDoc} * * @implSpec * This implementation iterates over the collection looking for the * specified element. If it finds the element, it removes the element * from the collection using the iterator's remove method. * * Note that this implementation throws an * {@code UnsupportedOperationException} if th.. 4-4) 상속보다는 컴포지션을 사용하라 상속 상속은 코드의 재사용성을 올려주고 유연한 개발을 가능하게 해주는 강력한 수단이다. 하지만 상속은 캡슐화를 깨뜨린다는 문제가 있다. 캡슐화는 클래스의 필드와 메소드의 동작방식을 외부에 숨기는 것을 말한다. 이를 통해 클래스와 외부 사용자는 서로에 대한 독립성을 가지고 서로에 대한 영향을 최소화할 수 있다. 하지만 상위 클래스를 상속받은 하위클래스는 상위클래스의 구현방식이 바뀌면 그에 따라 영향을 받는다. public class InstrumentedHashSet extends HashSet { private int addCount = 0; public InstrumentedHashSet() { } public InstrumentedHashSet(int initCap, float loadFactor) .. 이전 1 다음