[펌] http://blog.naver.com/syunge75?Redirect=Log&logNo=120137454086
1. Interface란
추상클래스와 final변수로만 이루어진 클래스이며 일반 클래스가 인터페이스를 구현해 주어야 상속이 가능하다.
클래스가 상속을 통해 구현하는데 한계가 있는 경우 자바에서 불가능한 다중상속을 흉내내기 위한 도구이다.
2. Interface의 특징
- 구현된 메소드는 올 수 없고 추상 메소드만 정의할 수 있다.
- 일반 클래스는 여러 interface를 다중 상속 할 수 있다.
- 인터페이스 간의 상속에서는 다중상속이 제공된다.
- 상속을 받은 일반 클래스는 interface의 추상메서드를 모두 재정의 해야 한다.
3. 사용방법
// 인터페이스 정의 public interface [인터페이스명]{ 상수형 변수;
// 다중 상속 public class [자식 클래스명B] implements[인터페이스명1], [인터페이스명2], ..., [인터페이스명n] { 필드; |
4. 예제 소스
//인터페이스 선언
public interface IBicycle {
//상수선언
int FrameSize_L = 18;
int FrameSize_M = 16;
int FrameSize_S = 15;
//추상메소드 선언
abstract void prtInfo();
}
public class Minivelo implements IBicycle{
//멤버변수 추가
int id;
String brand;
//오버라이딩 된 추상 메소드
public void prtInfo(){
System.out.println("ID : " +id);
System.out.println("Brand : " + brand);
//인터페에스에서 제공 하는 상수 사용
System.out.println("Frame Size : " + IBicycle.FrameSize_M);
}
public static void main(String[] args){
Minivelo mv = new Minivelo();
mv.id = 200;
mv.brand = "Dahon";
mv.prtInfo();
}
}
'Linux 일반 > Programming' 카테고리의 다른 글
Android - Button & Image ( 버튼을 이용한 이미지출력 ) (0) | 2011.12.07 |
---|---|
Unable to start activity ComponentInfo 에러 (0) | 2011.12.06 |
interface, implements (0) | 2011.12.01 |
androidmenifest.xml permission 종류 (0) | 2011.11.28 |
C와 JAVA 구조체 비교 (0) | 2011.11.23 |