25. 인터페이스(Interface)

박은서's avatar
Jan 01, 2026
25. 인터페이스(Interface)

1. 인터페이스(Interface)

교재 p.282-292 7.2 인터페이스, 7.3 인터페이스를 이용한 다중 상속

1️⃣ 인터페이스란?

클래스가 반드시 구현해야 할 “기능의 규약(약속)”을 정의한 것
📌 “무엇을 할 수 있는가”만 정의
📌 “어떻게 하는가”는 구현 클래스가 담당
※ 약속 = 상하관계의 약속 (갑이 쓰라는대로 쓰는 것)
행위를 제약, 강제 시킴으로 UX가 좋아짐

2️⃣ 인터페이스의 핵심 특징

항목
설명
객체 생성
❌ 불가
구현
implements 사용
다중 구현
⭕ 가능
메서드
기본적으로 추상 메서드
필드
public static final 상수만

3️⃣ 인터페이스 기본 문법

interface Flyable { void fly(); // 추상 메서드 }
class Bird implements Flyable { @Override public void fly() { System.out.println("난다"); } }

4️⃣ 인터페이스를 사용하는 이유

1) 다중 구현 지원

class SmartPhone implements Camera, Phone {}

2) 역할 중심 설계 (추상화)

interface Payment { void pay(); }

3) 결합도 감소 (DIP)

  • 구현 변경에 강함

4) 다형성

Payment p = new CardPay();

5️⃣ 인터페이스 vs 추상 클래스

구분
인터페이스
추상 클래스
다중 상속
필드
상수만
일반 변수 가능
생성자
구현
전부 강제(기본)
선택

6️⃣ 언제 인터페이스를 쓰나?

상황
선택
기능 규약 정의
인터페이스
다중 구현 필요
인터페이스
공통 코드 있음
추상 클래스

7️⃣ 실습

package ex07; // can do (할 수 있는 것만 정의해줌) interface 리모컨이할수있는것 { public abstract void power(); } abstract class 리모컨 implements 리모컨이할수있는것 { private boolean power; public 리모컨() { this.power = false; } public void power(){ this.power = !this.power; // !는 부정연산자 } public boolean isPower() { // getPower -> boolean타입은 isPower return power; } } class 엘지리모컨 extends 리모컨 { // extends할 경우 super();는 기본생성자에 자동으로 들어가있음 } class 삼성리모컨 extends 리모컨 { } // 인터페이스 (상하관계의 약속) public class InterApp { public static void main(String[] args) { 리모컨 r1 = new 삼성리모컨(); System.out.println(r1.isPower()); r1.power(); // 켜다 System.out.println(r1.isPower()); r1.power(); // 끄다 System.out.println(r1.isPower()); } }
notion image
Share article