OOP-Advanced02(Interface)
인터페이스 (Interface)
인터페이스란
- 클래스를 사용하는 방식, 접점만을 선언하는 클래스와 유사한 틀
- 아무런 구현이 되어 있지 않으며, 모든 메소드가 추상 메소드
인터페이스의 특징
class가 아닌interface키워드로 선언public또는default접근 제어자만 사용 가능
멤버 변수는 항상
public static final이며, 생략 가능멤버 메소드는 항상
public abstract이며, 생략 가능클래스와 달리 인터페이스는 여러 개 상속받을 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19public interface IFoo {
public static final int MEMBER_VAR = 10;
int MEMBER_VAR2 = 20; // public static final
public abstract void methodA(int param);
void methodB(int param); // public abstract
}
public class Foo implements IFoo {
void methodA(int param) {
System.out.println(param);
}
void methodB(int param) {
System.out.println(param);
}
}
인터페이스간의 상속
인터페이스 간의 ‘IS-A’ 관계
인터페이스가 인터페이스를 상속할 경우
extends로 상속클래스-클래스와 달리 다중 상속 가능
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28interface Walkable {
void walk();
}
interface Runnable {
void run();
}
public interface Jumpable extends Walkable, Runnable {
void jump();
}
public class Jumper implements Jumpable {
void walk() {
System.out.println("walk");
}
void run() {
System.out.println("run");
}
void jump() {
System.out.println("jump");
}
}
JDK 1.8
기본 메소드 (Default method): 자식 클래스에서 구현할 필요가 없는 메소드
- 인터페이스에
default메소드를 구현할 수 있다. default메소드는 항상public이다.- 인터페이스의 철학과 맞지 않으나, 인터페이스가 개선되었을 때 손쉽게 기능 추가를 위해 만들어짐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35interface IFoo {
void abstractMethod();
default void defaultMethod() { // 구현 클래스에서 구현할 필요가 없다.
System.out.println("Default method");
}
}
class SuperFoo {
void defaultMethod() {
System.out.println("Super method");
}
}
class FooOne implements IFoo {
void abstractMethod() {
return;
}
}
class FooTwo extends SuperFoo implements IFoo {
void abstractMethod() {
return;
}
}
public class Main {
public static void main(String[] args) {
FooOne fooOne = new FooOne();
fooOne.defaultMethod(); // Default method
FooTwo fooTwo = new FooTwo();
fooTwo.defaultMethod(); // Super method
}
}- 인터페이스에
static 메소드: 클래스 메소드와 동일하게 사용 가능
- 인터페이스 이름으로 호출 가능
- 클래스 구현체의 이름으로도 호출 가능
1
2
3
4
5
6
7
8
9
10
11interface IFoo {
static void staticMethod() {
System.out.println("static method");
}
}
public class Main {
public static void main(String[] args) {
IFoo.staticMethod(); // static method
}
}