Java 인터페이스

에어컨 (토론 | 기여)님의 2021년 10월 31일 (일) 19:07 판

개요

인터페이스는 다중상속을 지원
인터페이스는 추상 메소드와 상수만 있음
Class를 구현할때 기준이 되는 틀 ( 표준 ) 으로 사용할때 유용
  • 자동차를 이용한 인터페이스 개념
interface Car { 
	public abstract void Model(); 
}
class Bmw implements Car {
    public void Model() {
        System.out.println("BMW 320i");
    }
}
class Bentz implements Car {
    public void Model() {
        System.out.println("Bentz GLS200");
    }
}
public class Cars {
    public static void main(String[] args) {
        Bmw c = new Bmw();
        Bentz d = new Bentz();
        c.Model();
        d.Model();
    }
}
  • 인터페이스는 다중상속 가능
interface Car { public abstract void Model(); }
interface Fuel { public abstract void Resource(); }
class Bmw implements Car, Fuel {
    public void Model() {
        System.out.println("BMW 320i");
    }
    public void Resource() {
        System.out.println("Electronic");
    }
}
class Bentz implements Car, Fuel {
    public void Model() {
        System.out.println("Bebtz GLS200");
    }
    public void Fuel() {
        System.out.println("Disel");
    }
}
public class Cars {
    public static void main(String[] args) {
        Bmw c = new Bmw();
        Bentz d = new Bentz();
 
        c.Model();
        c.Resource();
        d.Model();
        d.Resource();
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}