Java
Interface
GOD03219
2017. 12. 29. 11:22
Interface
- 상수와 추상메소드로만 이루어져 있다.
- 다중 상속이 가능하다.
public interface Aa extends B { // 인터페이스끼리도 extends로 상속 가능, but 다중상속가능
final static int A = 10; // 상수니까 변수이름이 대문자이다. final은 생략가능, 상수는 선언과 동시에 초기화한다.
abstract void disp(); // 추상메소드 abstract 생략가능
}
public interface B {
abstract void disp2();
}
public class CC implements Aa, B { // 인터페이스를 상속받을 때는 implements 를 쓴다. 인터페이스는 다중상속이 가능하다.
@Override
public void disp2() {
}
public void disp() {
System.out.println("disp()"+A);
}
public static void main(String[] args) {
Aa a = new CC(); // 인터페이스는 객체를 만들지 못함
a.disp();
a.disp2(); // Aa가 B를 상속받았기 때문에 disp2 사용가능
}
}