Java/기본 문법
인터페이스(interface)
by 정구정구
2026. 6. 4.
인터페이스(interface)
- 설계 표준
- 클래스가 따라야할 최소한의 공통 규칙을 정의
- 세부 구현은 각 클래스에 맡김
인터페이스 적용
- implements 키워드로 활용 가능
- 인터페이스를 구현한 클래스를 구현체라고 함
interface Animal
{
void eat();
}
class Bird implements Animal
{
@Override
void eat() // 인터페이스 규칙
{
System.out.println("곡물을 먹는다");
}
void fly() // Bird 클래스만의 기능
{
System.out.println("하늘을 난다.");
}
}
class Dog implements Animal
{
@Override
void eat() // 인터페이스 규칙
{
System.out.println("사료를 먹는다");
}
void run() // Dog 클래스만의 기능
{
System.out.println("네 다리로 뛴다");
}
}
인터페이스 다중구현
interface Animal
{
void eat();
}
//새 인터페이스 정의
interface Flyable
{
void fly();
}
// 다중 구현으로 Bird 클래스 재정의
class Bird implements Animal, Flyable
{
public void eat()
{
System.out.println("곡물을 먹는다.");
}
public void fly()
{
System.out.println("하늘을 난다.");
}
}
인터페이스 다중상속
interface Animal
{
void eat();
}
interface Flyable
{
void fly();
}
// 다중 상속으로 새로운 인터페이스: 동물 + 나는 기능
interface FlyableAnimal extends Animal, Flyable
{
void land();
}
class Bird implements FlyableAnimal
{
public void eat()
{
System.out.println("곡물을 먹는다.");
}
public void fly()
{
System.out.println("하늘을 난다.");
}
public void land()
{
System.out.println("착륙한다.");
}
}
인터페이스 변수 선언
- public static final로 선언
- static 이기 때문에 구현체 없이도 활용 가능
- 변수 선언은 최소화 하는 것이 좋음