ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] Interface 나중 참고용
    java/Class.Interface.Method 2022. 2. 5. 21:05

    인터페이스 내부에 선언되는 것들은 모두 public 이다.

    선언 가능한 것들: 상수, 추상 메서드, 디폴트 메서드(java8), static 메서드(java8)


    코드 삽입 부분의 것은 축약된 형태이다. 

     

    1. 상수 정의

    interface TestInterface {
    
        int a = 100; //상수
    }

     

    모두 public static final로 간주된다.


    2. 추상 메서드 정의

    interface TestInterface {
        
        void abstractMethod(); //추상 메서드
    }

     

    모두 public abstract 로 간주된다.


    인터페이스의 추상 메서드를 모두 구현해야 하는 점이 있었다.

    java8부터 구현내용이 있는 static 메서드와 디폴트 메서드를 정의할 수 있다.

    두가지는 인터페이스 구현시 오버라이딩 없이 기존의 것을 바로 사용할 수 있다.


    3. 디폴트 메서드 (java8)

    interface TestInterface {
        
        default void defaultMethod(){ //디폴트 메서드
            System.out.println("디폴트 메서드");
        };
    
    }

     

     

    인터페이스 내부는 모두 public이다.

    추상 메서드가 정의된 것이 아님을 구분하기 위해 default 키워드를 필수로 선언해줘야 한다.

    위에서 사용된 default는 접근 제한자를 의미하는 default가 아닌 디폴트 메서드 문법에 해당하는 키워드이다.

    (public default의 경우라면 default는 접근제한자가 아닌 추상 메서드가 아닌 디폴트 메서드를 나타내는 키워드)


    4. static 메서드

    interface TestInterface {
        
        static void staticMethod(){ //static 메서드
            System.out.println("static 메서드");
        };
    
    }

     

     

    static을 사용하여 추상 메서드 디폴트 메서드 static 메서드를 구분할 수 있다.

    인스턴스 없이 호출할 수 있다.


    상수와 static 메서드는 객체와 관련이 없으므로 인터페이스명으로 사용가능

    interface TestInterface {
    
        int a = 100;
    
        static void sMethod() {
            System.out.println("static 메서드");
        }
    
    }
    
    
    public class InterfaceTest {
    
        public static void main(String[] args) {
    
            System.out.println(TestInterface.a);
            TestInterface.sMethod();
        }
    }


    default 메서드는 인스턴스 메서드이다. 기존의 것을 사용하거나 인터페이스 구현 클래스에서 재정의하여 사용할 수 있다.

    interface TestInterface {
    
        default void defMethod() {
            System.out.println("디폴트 메서드");
        }
    
    }
    
    class Imple implements TestInterface {
    }
    
    public class InterfaceTest {
    
        public static void main(String[] args) {
    
            Imple imple = new Imple();
            imple.defMethod();
        }
    }

     

    interface TestInterface {
    
        default void defMethod() {
            System.out.println("디폴트 메서드");
        }
    
    }
    
    class Imple implements TestInterface {
    
        @Override
        public void defMethod() {
            System.out.println("디폴트 메서드 오버라이딩");
        }
    }
    
    public class InterfaceTest {
    
        public static void main(String[] args) {
    
            Imple imple = new Imple();
            imple.defMethod();
        }
    }


    참고로 static 메서드의 경우 동일한 이름으로 정의하는 것은 오버라이딩이 아닌 별도의 메서드를 정의하는 것에 해당한다. 

     

    예제는 아래 포스팅 참고

    https://hellose7.tistory.com/141

    'java > Class.Interface.Method' 카테고리의 다른 글

    [Java] 클래스 상속관계에서의 static 메서드 재정의  (0) 2022.02.06
    [Java] Wrapper class 관련  (0) 2021.12.16
    [Java] 내부 클래스  (0) 2021.08.29
    [Java] 익명 클래스  (0) 2021.08.28

    댓글

Designed by Tistory.