We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Originally posted by JoisFe March 13, 2023
매개변수 타입으로 클래스가 아니라 인터페이스를 사용하라
객체는 클래스가 아닌 인터페이스로 참조하라
// 좋은 예 - 인터페이스를 타입으로 사용 Set<Son> sonSet = new LinkedHashSet<>(); // 나쁜 예 - 클래스를 타입으로 사용 LinkedHashSet<Son> sonSet = new LinkedHashSet<>();
PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1);
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Discussed in https://github.com/orgs/Study-2-Effective-Java/discussions/151
Originally posted by JoisFe March 13, 2023
아이템 64. 객체는 인터페이스를 사용해 참조하라
어디서 많이 본 주제인데?
매개변수 타입으로 클래스가 아니라 인터페이스를 사용하라
와 비슷한 느낌이 난다.객체는 클래스가 아닌 인터페이스로 참조하라
로 확장 가능적합한 인터페이스만 있다면 매개변수 뿐 아니라 반환값, 변수, 필드를 전부 인터페이스 타입으로 선언하라!!!!!!!!
인터페이스를 타입으로 사용하는 습관을 길러두면 프로그램이 훨씬 유연해짐
주의할 점
Ex)
구현 타입을 바꾸려 하는 동기?
오해
적합한 인터페이스가 없다면 당연히 클래스로 참조해야 함!
1. String, BigInteger가 대표적인 예
2. 클래스 기반으로 작성된 프레임워크가 제공하는 객체들
3.인터페이스에는 없는 특별한 메서드를 제공하는 클래스들
참고 (PriorityQueue)
클래스 타입을 직접 사용하는 경우는 이러한 추가 메서드를 꼭 사용해야 하는 위 3가지 정도의 경우로 최소화 해야함
정리
The text was updated successfully, but these errors were encountered: