인터페이스 vs 추상 클래스
발행: (2026년 1월 18일 오후 02:24 GMT+9)
3 min read
원문: Dev.to
Source: Dev.to
인터페이스 또는 추상 클래스?
두 개념은 매우 유사하기 때문에 언제 각각을 사용해야 할지 혼란스러울 수 있습니다. 이 글에서 그 질문에 답하고자 합니다.
짧은 답변
인터페이스를 사용할 때…
- 기본 동작이 없을 때
- 하위 클래스(구현체)가 해당 동작을 반드시 구현하도록 강제하고 싶을 때
추상 클래스를 사용할 때…
- 기본 동작을 정의하고 싶을 때
- 프로세스 흐름을 제어하고, 개별 단계는 하위 클래스가 구현하도록 하고 싶을 때
코드 예시
인터페이스
public interface IAnimal {
void bark();
}
public class Dog implements IAnimal {
@Override
public void bark() {
System.out.println("woof!");
}
}
public static void main(String[] args) {
IAnimal dog = new Dog();
dog.bark(); // "woof!"
}
짖는 소리는 동물마다 다르기 때문에 IAnimal은 기본 동작을 제공하지 않습니다. 하위 클래스는 bark 메서드를 반드시 오버라이드해야 하며, 이를 하지 않으면 컴파일 오류가 발생합니다.
추상 클래스
public abstract class Animal {
// The method is final to prevent subclasses from changing the order in which methods are called
public final void bark() {
takeBreath();
makeSounds();
}
// private method as implementation is common for all the animals
private void takeBreath() {
System.out.println("taking a deep breath...");
}
// protected so that subclasses can override while client (main method) cannot call this method directly
// abstract method to force subclasses to implements this method
protected abstract void makeSounds();
}
public class Cat extends Animal {
@Override
public void makeSounds() {
System.out.println("meow!");
}
}
public static void main(String[] args) {
Animal car = new Cat();
cat.bark(); // "meow!"
}
makeSounds 프로세스를 제어하여 모든 동물이 먼저 숨을 들이쉬고 그 다음에 짖도록 하고 싶습니다; 이 흐름은 일정하게 유지되어야 합니다. 그러나 앞 예시와 마찬가지로 짖는 소리는 동물마다 다르므로 bark 메서드는 하위 클래스에서 오버라이드되어야 합니다.