대문자로 시작$와 _를 제외한 특수 문자 사용xpublic interface 인터페이스명 {...} interface 인터페이스명 {
//상수
타입 상수명 = 값;
//추상 메소드
타입 메소드명(매개변수, ...);
//디폴트 메소드
default 타입 메소드명(매개변수, ...){...}
//정적 메소드
static 타입 메소드명(매개변수, ...){...}
}
public static final의 특징을 가짐public interface RemoteControl{
public int MAX_VOLUME = 10; // 컴파일시 public static final로 자동 변환
public int MIN_VOLUME = 0; // 컴파일시 public static final로 자동 변환
}
public interface RemoteControl{
public void turnOn();
public void turnOff();
public void setVolume(int volume);
// 컴파일시 모든 메소드에 자동적으로 public abstract가 붙음
}
[public] default 리턴타입 메소드명(매개변수, ...){...}
default 키워드를 반드시 붙여야 함public 접근 제한을 가지므로 생략하더라도 컴파일 과정에서 붙음public interface RemoteControl{
default void setMute(boolean mute){
if(mute){
System.out.println("무음 처리합니다.");
}else{
System.out.println("무음 해제합니다.");
}
}
}
[public] static 리턴타입 메소드명(매개변수, ...) {...}
public interface RemoteControl{
static void changeBattery(){
System.out.println("건전지를 교환합니다.");
}
}
오픈 소스코드를 만들었는데 그 오픈소스가 엄청 유명해져서 전 세계 사람들이 다 사용하고 있는데, 인터페이스에 새로운 메소드를 만들어야 하는 상황이 발생했다.
자칫 잘못하면 내가 만든 오픈소스를 사용한 사람들은 전부 오류가 발생하고 수정을 해야 하는 일이 발생할 수도 있다.
이런 경우를 위해 만들어짐
public class 구현클래스명 implements 인터페이스명 {
//인터페이스에 선언된 추상 메소드의 실체 메소드 선언
}
추상 메소드의 실체 메소드를 작성하는 방법abstract)로 선언해야 함// 추상 클래스
public abstract class Television implements RemoteControl{
public void turnOn() {...}
public void turnOff() {...}
// setVolume() 메소드를 오버라이딩 하지 않음 (추상 클래스로 선언)
}
public 접근 제한을 갖기 때문에 public 보다 더 낮은 접근 제한으로 작성 불가@Override 어노테이션을 이용해서 정확히 재정의되었는지 컴파일러가 체크하도록 함Television tv = new Television();
인터페이스 변수 = 구현객체;
RemoteControl rc = new Television();
// RemoteControl 인터페이스 변수 rc에
// RemoteControl 인터페이스를 구현해놓은 Television 클래스를 대입
인터페이스 변수 = new 인터페이스() {
// 인터페이스에 선언된 추상 메소드의 실체 메소드 선언
}
클래스$번호.class 파일명으로 생성됨public class 구현 클래스명 implements 인터페이스1, 인터페이스2{
// 인터페이스1에 선언된 추상 메소드의 실체 메소드 선언
// 인터페이스2에 선언된 추상 메소드의 실체 메소드 선언
}
public class MyClass{
//필드
RemoteControl rc = new Television(); // 인터페이스 변수에 구현 객체 대입
//생성자
// 생성자에 매개변수로 인터페이스 변수가 옴 (구현 객체 대입 가능)
MyClass(RemoteControl rc){
this.rc = rc;
}
//메소드
void methodA(){
//로컬 변수
RemoteControl rc = new Audio(); // 인터페이스 변수에 구현 객체 대입
}
void methodB(RemoteControl rc) {...}
// 메소드에 매개변수로 인터페이스 변수가 옴 (구현 객체 대입 가능)
}
RemoteControl rc = new Television();
rc.turnOn(); // Television 클래스의 실체 메소드 turnOn() 실행
rc.turnOff(); // Television 클래스의 실체 메소드 turnOff() 실행
public interface RemoteControl {
default void setMute(boolean mute){...} // 디폴트 메소드
}
RemoteControl.setMute(true); // 불가능
RemoteControl rc = new Television(); // 구현 객체가 인터페이스 변수에 대입됨
rc.setMute(true); // 가능
public interface RemoteControl {
static void setMute(boolean mute){...} // 정적 메소드
}
RemoteControl.setMute(true); // 가능
interface A {
void method1();
void method2();
}
A a = new ClassEx1();
A a = new ClassEx2();
a.method1();
a.method2();
// 수정 필요 없음
인터페이스 변수 = 구현객체;
// 클래스가 인터페이스로 자동 타입 변환됨

// Tire 인터페이스 생성
public interface Tire {
public void roll();
}
// Tire 인터페이스를 구현한 HankookTire 구현 클래스
public class HankookTire implements Tire {
@Override
public void roll(){
System.out.println("한국 타이어가 굴러갑니다.")
}
}
public class Car {
Tire frontLeftTire = new HankookTire();
Tire frontRightTire = new HankookTire();
Tire backLeftTire = new HankookTire();
Tire backRightTire = new HankookTire();
// 인터페이스 변수에 구현 클래스를 대입
// (클래스->인터페이스 자동 타입 변환)
void run(){
frontLeftTire.roll();
frontRightTire.roll();
backLeftTire.roll();
backRightTire.roll();
// System.out.println("한국 타이어가 굴러갑니다.")가 실행 됨
}
}
Car myCar = new Car();
myCar.frontLeftTire = new KumhoTire();
myCar.frontRightTire = new KumhoTire();
// 인터페이스 변수에 구현 클래스를 대입
// run() 메소드 실행시
// System.out.println("금호 타이어가 굴러갑니다.")가 실행 됨
Tire[] tires = {
new HankookTire(),
new HankookTire(),
new HankookTire(),
new HankookTire()
};
tires[1] = new KumhoTire();
// Vehicle 인터페이스 생성
public interface Vehicle {
public void run();
}
public class Driver {
// Vehicle 인터페이스를 매개변수로 받는 drive() 메소드
public void drive(Vehicle vehicle){
vehicle.run();
}
}
Driver driver = new Driver(); // Driver 클래스 객체 생성
Bus bus = new Bus(); // Vehicle 인터페이스를 구현한 bus 구현 객체 생성
driver.drive(bus); // = Vehicle vehicle = bus;
// bus 구현 객체의 run() 메소드 실행
구현 클래스 변수 = (구현 클래스) 인터페이스 변수;
interface Vehicle {
void run();
}
class Bus implements Vehicle {
void run() {...} // Vehicle 인터페이스의 추상 메소드 구현
void checkFare() {...} // 메소드 선언
}
Vehicle vehicle = new Bus(); // 구현 객체를 인터페이스 변수에 대입
vehicle.run(); // 가능
vehicle.checkFare(); // 불가능
Bus bus = (Bus)vehicle; // Bus 클래스로 강제 타입 변환
vehicle.run(); // 가능
vehicle.checkFare(); // 가능
public void method(Vehicle vehicle){
if(vehicle instanceof Bus){ // vehicle가 Bus객체를 참조하고 있는지 확인
Bus bus = (Bus)vehicle;
}
}
public interface 하위 인터페이스 extends 상위 인터페이스1, 상위 인터페이스2 {...}
하위 인터페이스 변수 = new 구현 클래스();
상위 인터페이스1 변수 = new 구현 클래스();
상위 인터페이스2 변수 = new 구현 클래스();
public interface ParentInterface {
public void method1();
public default void method2(){
System.out.println("default method");
}
}
public interface ChildInterface3 extends ParentInterface {
public void method2();
// default를 없애고 추상 메소드로 선언
}