song.log

[자바 기술면접] 14. 내부 클래스, 익명 클래스 본문

StudyLog/Java interview

[자바 기술면접] 14. 내부 클래스, 익명 클래스

SingaKorean 2023. 4. 29. 16:35
728x90
반응형

- 정의 

내부 클래스(inner class) :  다른 클래스 내부에 선언된 클래스입니다. 내부 클래스는 자신을 감싸는 외부 클래스의 변수와 메서드에 직접 접근할 수 있습니다.

키워드 : 클래스 내부에 선언된 클래스, 패키지 간소화, 캡슐화 강화, 직관적인 가독성

 

익명 클래스(anonymous class) :  이름이 없는 내부 클래스로, 클래스 정의와 동시에 인스턴스를 생성하는 방식입니다. 주로 인터페이스나 추상 클래스를 상속받아 사용됩니다. 익명 클래스는 한 번만 사용될 때 유용하며, 코드의 간결성을 높이는 데 도움을 줍니다.

키워드 : 클래스 정의와 객체화를 동시 처리, 일회성 사용, 익명 클래스 끝엔 세미콜론, 오버라이딩한 메소드만 사용가능

 

- 영어 정리 : 

Inner class : a class that is declared inside another class. An inner class can access the variables and methods of the enclosing outer class.

Anonymous class : an unnamed inner class that is defined and instantiated at the same time. It is often used to implement interfaces or abstract classes. Anonymous classes are useful when they are only used once and can help make the code more concise.

 

 

728x90
반응형
Comments