개요
- Java interface
- 자바 인터페이스
- 인터페이스는 다중상속을 지원한다.
- 인터페이스는 추상 메소드와 상수만 있다.
- Class를 구현할 때 기준이 되는 틀로 사용할 때 유용하다.
예시 1
interface Animal {
public void animalSound();
public void sleep();
}
class Dog implements Animal {
public void animalSound() {
System.out.println("bark");
}
public void sleep() {
System.out.println("zzz");
}
}
public class App {
public static void main(String args[]) {
Animal d = new Dog();
d.animalSound();
d.sleep();
}
}
예시 2
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 App {
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 Resource() {
System.out.println("Disel");
}
}
public class App {
public static void main(String[] args) {
Bmw c = new Bmw();
Bentz d = new Bentz();
c.Model();
c.Resource();
d.Model();
d.Resource();
}
}
같이 보기