인터페이스의 메서드는 기본적으로 추상입니다. 즉, 인터페이스의 메서드에는 메서드 서명만 있고 내부에는 내용이 없습니다. 예를 들어 보겠습니다 -
예
interface Car{
public void carSpeed();
public void sleep();
}
class Porsche implements Car{
public void carSpeed(){
System.out.println("The speed of the Porsche is too much");
}
public void sleep(){
System.out.println("Sleeping for few milliseconds");
}
}
public class Demo{
public static void main(String[] args){
Porsche my_car = new Porsche();
my_car.carSpeed();
my_car.sleep();
}
} 출력
The speed of the Porsche is too much Sleeping for few milliseconds
'Car'라는 인터페이스는 'carSpeed'와 'sleep'이라는 두 개의 함수로 정의됩니다. Npw, 이 인터페이스는 'Porsche'라는 클래스에 의해 구현됩니다. 이 클래스는 'carSpeed'와 'sleep'을 정의하는 반면 인터페이스는 방금 정의했고 본문이 없었습니다. 이제 Demo라는 클래스에는 Porsche 클래스의 인스턴스를 생성하는 주요 함수가 포함되어 있습니다. 이 인스턴스는 'carSpeed' 및 'sleep' 함수에서 호출됩니다.