내부클래스
: 클래스 내부에 생성된 클래스
class OuterClass {
class InnerClass {
}
}
내부클래스 사용 목적
클래스 간의 관계가 긴밀할 때 코드를 간결하게 하기 위해 사용
내부클래스의 종류
: 내부 클래스는 선언된 위치에 따라 그 종류가 달라짐
인스턴스 클래스
외부 클래스에서 멤버 변수 위치에 선언
클래스의 내부에서 인스턴스(객체) 멤버처럼 다뤄지며, 인스턴스 멤버들과 상호작용 가능
class OuterClass {
class InnerClass {
...
}
}
스태틱 클래스
외부 클래스에서 멤버 변수 위치에 선언
클래스 내부에서 static 멤버처럼 다뤄지며 static 멤버들과 상호작용 가능
class OuterClass {
static class InnerClass {
...
}
}
지역클래스
메서드 내부 지역변수 위치에 선언
메서드나 초기화 블록의 내부에서 다뤄지며 선언된 영역 내부에서 사용
class OuterClass {
void A (){
class InnerClass {
...
}
}
}
메모리 영역 중 스택 영역에 존재하기 때문에 메서드 호출~종료까지 메모리에 존재하는 영역
익명클래스
이름이 없는 클래스
기존 클래스를 오버라이딩 해서 만든다
class Some {
private int a=3;
int getter(){
return this.a;
}
void setter(int a) {
this.a=a;
}
}
Some annony = new Some(){
private int a=3;
int getter(){
return this.a;
}
void setter(int a) {
this.a=a;
}
};
ex.
public class Anonymous {
public static void main(String[] args) {
OuterClass1 o = new OuterClass1() {
void a() {
System.out.println("새롭게 정의된 익명클래스 메서드입니다");
}
};
o.a();
OuterClass1 ok = new OuterClass1();
ok.a(); // 익명클래스는 1회성
}
}
class OuterClass1 {
void a() {System.out.println("method a");}
void b() {}
}
내부클래스 특성
내부클래스도 클래스이기 때문에 선언부에 접근제어자를 사용할 수 있다
class OuterClass {
private class InnerClass {
...
}
protected class InnerClass {
...
}
}
내부 클래스의 접근성
Exercise 1
public class InnerClass {
public static void main(String[] args) {
}
}
class A {
static class StaticInner {
}
class InstanceInner {
}
StaticInner st = new StaticInner();
InstanceInner ii = new InstanceInner();
static void StaticMethod () {
StaticInner st2 = new StaticInner();
// InstanceInner ii2 = new InstanceInner(); -> static메서드는 인스턴스 멤버에 접근 불가능
}
void InstanceMethod () {
StaticInner st3 = new StaticInner();
InstanceInner ii3 = new InstanceInner();
// 인스턴스 메서드는 스태틱 클래스, 인스턴스 클래스 모두 접근 가능
}
// 스태틱 클래스와 인스턴스 클래스 두 클래스 모두 객체 생성 가능
}
Exercise 2
public class Anonymous2 {
public static void main(String[] args) {
// Inter1 it = new Inter(); // 인터페이스는 객체를 생성할 수 없다
Inter1 it = new Inter1() { // 익명 클래스를 이용해 일회성으로 사용할 수 있다
@Override
public void f1() {
System.out.println("inter1 imple f1()");
}
};
it.f1();
}
}
interface Inter1 {
void f1();
}
class Inter1Impl implements Inter1 {
@Override
public void f1() {
}
}
public class Anonymous3 {
public static void main(String[] args) {
// Abstract ab = new Abstract(); // 인터페이스 객체 생성 불가
Abstract ab = new Abstract() {
@Override
void f2() {
System.out.println("Abstract extends f2();");
}
};
ab.f2();
}
}
abstract class Abstract {
abstract void f2();
}
JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63
JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71
JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74
JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78
JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80
JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84
JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88
JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93
JAVA Eclipse 09 제어문 : 반복문 for
https://developernew.tistory.com/102
JAVA Eclipse 10 제어문 : 반복문 while, do-while
https://developernew.tistory.com/103
JAVA Eclipse 11 제어문 : 반복문의 break, continue
https://developernew.tistory.com/106
JAVA Eclipse 12 배열 : 배열의 개념 및 사용
https://developernew.tistory.com/107
JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기
https://developernew.tistory.com/115
JAVA Eclipse 14 배열 : 다차원배열
https://developernew.tistory.com/117
JAVA Eclipse 15 배열의 복제, for ~ each문
https://developernew.tistory.com/120
JAVA Eclipse 16 카페 주문 시스템(키오스크) 배열과 제어문(반복문, 조건문)으로 풀기
https://developernew.tistory.com/124
JAVA Eclipse 17 method 메서드(메소드)
https://developernew.tistory.com/126
JAVA Eclipse 18 메서드(메소드) 오버로딩
https://developernew.tistory.com/133
JAVA Eclipse 19 객체지향 언어
https://developernew.tistory.com/135
JAVA Eclipse 20 클래스와 객체
https://developernew.tistory.com/155
JAVA Eclipse 21 인스턴스 변수와 클래스 변수
https://developernew.tistory.com/156
JAVA Eclipse 22 객체 타입 배열
https://developernew.tistory.com/157
JAVA Eclipse 23 클래스 생성자
https://developernew.tistory.com/158
JAVA Eclipse 24 상속과 다형성 - 상속/메서드 오버라이딩
https://developernew.tistory.com/159
JAVA Eclipse 25 상속과 다형성 - 다형성 개념과 다형성 관련 실습
https://developernew.tistory.com/160
JAVA Eclipse 26 추상메서드와 추상클래스
https://developernew.tistory.com/161
JAVA Eclipse 27 인터페이스
https://developernew.tistory.com/162
'Backend > JAVA2 멘토시리즈' 카테고리의 다른 글
JAVA Eclipse 30 예외처리 (0) | 2023.07.27 |
---|---|
JAVA Eclipse 29 예외처리 - 예외/예외처리 개념 (0) | 2023.06.27 |
JAVA Eclipse 27 인터페이스 (0) | 2023.05.12 |
JAVA Eclipse 26 추상메서드와 추상클래스 (0) | 2023.05.11 |
JAVA Eclipse 25 상속과 다형성 - 다형성 개념과 다형성 관련 실습 (1) | 2023.05.11 |