์ธํฐํ์ด์ค๋ ๊ฐ์ฒด๊ฐ ๋ฐ๋์ ๊ตฌํํด์ผ ํ ๊ธฐ๋ฅ์ ๊ท์ฝ(๊ณ์ฝ)์ ์ ์ํ๋ ๊ตฌ์กฐ๋ค.
ํด๋์ค๊ฐ ์ค๊ณ๋๋ผ๋ฉด, ์ธํฐํ์ด์ค๋ ๊ธฐ๋ฅ ๋ช ์ธ์์ ๊ฐ๊น๋ค.
1. ์ธํฐํ์ด์ค๋?
interface Animal {
void sound();
}
ํน์ง
- interface ํค์๋ ์ฌ์ฉ
- ๋ฉ์๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก public abstract
- ๊ฐ์ฒด ์์ฑ ๋ถ๊ฐ
2. ๊ตฌํ(implements)
์ธํฐํ์ด์ค๋ ์์์ด ์๋๋ผ ๊ตฌํํ๋ค.
class Dog implements Animal {
@Override
public void sound() {
System.out.printIn("๋ฉ๋ฉ");
}
}
- implements ํค์๋ ์ฌ์ฉ
- ๋ชจ๋ ์ถ์ ๋ฉ์๋๋ฅผ ๋ฐ๋์ ๊ตฌํํด์ผ ํจ
3. ์ธํฐํ์ด์ค์ ๋คํ์ฑ
์ธํฐํ์ด์ค๋ ๋คํ์ฑ์ ์ ๊ณตํ๋ค.
Animal a = new Dog();
a.sound();
- ์ฐธ์กฐ ํ์ → ์ธํฐํ์ด์ค
- ์ค์ ๊ฐ์ฒด → ๊ตฌํ ํด๋์ค
์ธํฐํ์ด์ค๋ ๋คํ์ฑ์ ์ํ ๊ฐ์ฅ ๊ฐ๋ ฅํ ๋๊ตฌ๋ค.
4. ๋ค์ค ๊ตฌํ ๊ฐ๋ฅ
ํด๋์ค๋ ๋จ์ผ ์์๋ง ๊ฐ๋ฅํ์ง๋ง, ์ธํฐํ์ด์ค๋ ์ฌ๋ฌ ๊ฐ ๊ตฌํํ ์ ์๋ค.
interface Flyable {
void fly();
}
interface Swimable {
void swim();
}
class Duck implements Flyable, Swimable {
public void fly() {}
public void swim() {}
}
5. default ๋ฉ์๋
interface Animal {
default void breathe() {
System.out.printIn("์จ ์ฐ๋ค.");
}
}
- default ํค์๋ ์ฌ์ฉ
- ๊ตฌํ ํด๋์ค์์ ์ฌ์ ์ ๊ฐ๋ฅ
6. static ๋ฉ์๋
interface Animal {
static void info() {
Systme.out.printIn("๋๋ฌผ ์ธํฐํ์ด์ค");
}
}
์ธํฐํ์ด์ค ์ด๋ฆ์ผ๋ก ์ง์ ํธ์ถํ๋ค.
Animal.info();
7. ํ๋
์ธํฐํ์ด์ค์ ๋ณ์๋ ์๋์ผ๋ก ๋ค์ ์์ฑ์ ๊ฐ์ง๋ค.
- public
- static
- final
์ฆ, ์์๋ง ์ ์ ๊ฐ๋ฅํ๋ค.
int MAX = 10;
8. ์ถ์ ํด๋์ค vs. ์ธํฐํ์ด์ค
| ๊ตฌ๋ถ | ์ถ์ ํด๋์ค | ์ธํฐํ์ด์ค |
| ํค์๋ | abstract class | interface |
| ๋ค์ค ์์ | ๋ถ๊ฐ๋ฅ | ๊ฐ๋ฅ |
| ํ๋ | ์ผ๋ฐ ํ๋ ๊ฐ๋ฅ | ์์๋ง ๊ฐ๋ฅ |
| ๋ชฉ์ | ๋ถ๋ถ ๊ตฌํ + ๊ณตํต ์ํ | ๊ธฐ๋ฅ ๊ท์ฝ ์ ์ |
| ์์ฑ์ | ์กด์ฌ | ์์ |
9. ์ค๊ณ ๊ด์ ์์์ ์ฐจ์ด
1) ์ถ์ ํด๋์ค
- "~์ด๋ค(is-a)" ๊ด๊ณ
- ๊ฐํ ๊ฒฐํฉ
- ๊ณตํต ์ํ ๊ณต์
2) ์ธํฐํ์ด์ค
- "~ํ ์ ์๋ค(can-do)" ๊ด๊ณ
- ๋์จํ ๊ฒฐํฉ
- ์ญํ ์ค์ฌ ์ค๊ณ
3) ์์
- Dog is an Animal → ์์
- Dog can Fly → ์ธํฐํ์ด์ค
10. ์ ์ธํฐํ์ด์ค๊ฐ ์ค์ํ๊ฐ?
์ค๋ฌด์์๋ ์ธํฐํ์ด์ค ๊ธฐ๋ฐ ์ค๊ณ๋ฅผ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ค.
์ด์
- ๊ตฌํ๊ณผ ๋ถ๋ฆฌ
- ๊ต์ฒด ๊ฐ๋ฅ์ฑ ํ๋ณด
- ํ ์คํธ ์ฉ์ด
- ์ ์ง๋ณด์ ํธ๋ฆฌ
ex.
List<String> list = new ArrayList<>();
์ผ์ชฝ์ ์ธํฐํ์ด์ค, ์ฐ์ธก์ ๊ตฌํ์ฒด
์ด ๊ตฌ์กฐ๊ฐ Java ์ค๊ณ์ ๊ธฐ๋ณธ ํจํด์ด๋ค.
'Back-End > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| 11. ์ปฌ๋ ์ ํ๋ ์์ํฌ(Collection Framework) (0) | 2026.02.12 |
|---|---|
| 10. ๊ธฐ๋ณธ ํด๋์ค(Core Class) (0) | 2026.02.12 |
| 08. ์ถ์ ํด๋์ค(Abstract Class) (0) | 2026.02.11 |
| 07. ์์๊ณผ ๋คํ์ฑ (0) | 2026.02.11 |
| 06. ๋ฐฐ์ด๊ณผ ArrayList (0) | 2026.02.11 |