자바 인터페이스 interface
package test;
interface Animalable {
// 모든 멤버는 public 접근지정자가 옴
// static final 상수 필드만 올 수 있음
int RED =1;
public static final int BLUE = 2;
// 모든 메소드는 추상메소드만 올 수 있음
void speak();
public abstract void eat();
// 인터페이스는 다중상속 가능
}
class Puppy2 implements Animalable {
@Override
public void speak() {
System.out.println("멍멍~");
}
@Override
public void eat() {
System.out.println("강아지가 밥을 먹는다");
}
}
public class Test7 {
public static void main(String[] args) {
// interface 키워드. 인터페이스=규격=약속
System.out.println(Animalable.RED);
//Animalable.RED=10; // 상수이므로 수정 안됨
System.out.println(Animalable.BLUE);
Animalable ani;
ani = new Puppy2();
ani.speak();
// ani = new Animalable(); // 인터페이스는 객체생성 용도 아님
}
}