ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 인터페이스(interface)
    spring.project.log 2022. 3. 17. 15:28

    인터페이스에 대해 이해해보려고 쓴 글입니다.


    reference https://www.w3schools.com/java/java_interface.asp

     

    Java Interface

    W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

    www.w3schools.com

    An interface is a completely "abstract class" that is used to group related methods with empty bodies:

    나는 인터페이스라는 단어를 9살 때 Why? 컴퓨터책을 읽다가(ㅋㅋ) 프린터기와 컴퓨터를 연결하기 위해 필요한 것이라고 들어보았다. 프린터기의 인터페이스를 알아야 컴퓨터와 연결시킬 수 있었기 때문에 영어의 어원을 몰라도 인터페이스가 "연결"시킨다는 의미를 가지고 있는 것은 추측할 수 있었다.
    일반론적으로 인터페이스라는 것은 서로 다른 무언가를 맞닿아서 연결시키는(inter) 면(face)을 말한다. 그렇다면 서로 다른 어떤 걸 연결시킨다는 걸까?

    W3schools에 따르면, 인터페이스의 정체는 "추상클래스"이다. 인터페이스는 관련 메서드(related method)를 빈 본문(empty body)으로 그룹화하기 위해 사용된다.

    An interface is a completely "abstract class" that is used to group related methods with empty bodies:

    // interface
    interface Animal {
      public void animalSound(); // interface method (does not have a body)
      public void run(); // interface method (does not have a body)
    }

     

     
    자바는 다중상속을 지원하지 않기 때문에* 인터페이스가 필요하다. 즉 추상클래스로 정의된 부모클래스와 자식클래스로 정의된 실체클래스를 "상속"으로 연결하려고 할 때 자식클래스는 하나의 수퍼클래스(부모)를 가질 수 밖에 없다. 그래서 여러 개의 추상클래스와 하나의 실체클래스를 연결하기 위해  implement라는 예약어를 사용하는 것이다.

    * ) 메소드의 출처가 모호해지기 때문

    즉, "인터페이스"는 자식클래스와 추상클래스를 연결시켜주는 (조금 더 강제적인) 추상클래스이다. 이 때 클래스들을 연결시키는 방법이 implement 하는 것이다. 

    2) Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

     

    interface FirstInterface {
      public void myMethod(); // interface method
    }
    
    interface SecondInterface {
      public void myOtherMethod(); // interface method
    }
    
    class DemoClass implements FirstInterface, SecondInterface {
      public void myMethod() {
        System.out.println("Some text..");
      }
      public void myOtherMethod() {
        System.out.println("Some other text...");
      }
    }
    
    class Main {
      public static void main(String[] args) {
        DemoClass myObj = new DemoClass();
        myObj.myMethod();
        myObj.myOtherMethod();
      }
    }

    인터페이스는 추상클래이스이기 때문에 객체를 생성할 수 없다.

    • Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "Animal" object in the MyMainClass)

    vs 추상클래스

    자바에서 추상 클래스는 추상 메소드뿐만 아니라 생성자, 필드, 일반 메소드도 포함할 수 있다.

    하지만 인터페이스(interface)는 오로지 추상 메소드와 상수만을 포함할 수 있다.

     

    'spring.project.log' 카테고리의 다른 글

    [JAVA] 접근제어자, getter/setter  (0) 2022.03.26
    오버라이딩(overriding)과 오버로딩(overloading)  (0) 2022.03.18
    이넘(enum)  (0) 2022.03.17
    추상클래스(abstract class)  (0) 2022.03.17
    spring 첫회의  (0) 2022.03.04
Designed by Tistory.