Java에서 class를 추상화한다 

interface를 추상화된 class로 보면 된다

interface method는 implement된 class에 의해 만들어진다

 

// Interface
interface Animal {
  public void animalSound(); // interface method (body를 가지고 있지 않음)
  public void sleep();      // interface method (body를 가지고 있지 않음)
}

// Pig "implements" the Animal interface
class Pig implements Animal {
  public void animalSound() {
    // The body of animalSound() is provided here
    System.out.println("The pig says: wee wee");
  }
  public void sleep() {
    // The body of sleep() is provided here
    System.out.println("Zzz");
  }
}

class MyMainClass {
  public static void main(String[] args) {
    Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}
//실행 결과 값
The pig says: wee wee
Zzz

 

interface의 특징

-interface는 객체를 만들 수 없다

-interface method의 디폴트 값은 abstract와 public이다

-interface의 속성 값은 public, static과 final이 디폴트 값이다

 

 

 

 

출처 : https://www.w3schools.com/java/java_interface.asp

 

+ Recent posts