
⚠️ 이 글은 저작권 이슈로 인해 일부 내용이 수정 또는 제거되었습니다.
public interface Animal {
void sound(); // 추상 메서드
}public interface Animal {
void sound(); // 추상 메서드
}public class Dog implements Animal {
public void sound() {
System.out.println("멍멍");
}
}public class Dog implements Animal {
public void sound() {
System.out.println("멍멍");
}
}implements로 구현public class RobotDog implements Animal, Machine { ... }public class RobotDog implements Animal, Machine { ... }Animal a = new Dog();
a.sound(); // Dog의 구현체에 따라 실행됨Animal a = new Dog();
a.sound(); // Dog의 구현체에 따라 실행됨public interface Animal {
default void breathe() {
System.out.println("숨쉰다");
}
static void info() {
System.out.println("모든 동물은 살아있다");
}
}public interface Animal {
default void breathe() {
System.out.println("숨쉰다");
}
static void info() {
System.out.println("모든 동물은 살아있다");
}
}default 메서드는 오버라이딩 가능, static 메서드는 클래스명으로 호출| 항목 | 인터페이스 | 추상 클래스 |
| 상속 | 다중 구현 가능 | 단일 상속만 가능 |
| 구성 | 상수 + 추상 메서드 (+default/static) | 변수 + 생성자 + 메서드 포함 가능 |
| 목적 | 동작 규약 정의 | 공통 코드 재사용 + 설계 템플릿 |
🔑 인터페이스는 "이런 기능이 있어야 해!"🔧 추상 클래스는 "공통 기능도 줄게!"