본문 바로가기
디자인패턴

인터페이스 메서드

by 상국이 2022. 4. 2.
728x90

* default 메서드

 > 인터페이스에 기본 구현체를 만들 수 있음

 > 그 인터페이스를 구현하는 클래스, 상속받은 다른 인터페이스도 해당 기능을 사용할 수 있음

public interface SampleInterface {

   default public Sample setDefault(ID id) {
       //기능 구현이 가능
   }   
}

 

* static 메서드

 > 모든 인스턴스가 공유할 수 있는 static메소드를 인터페이스에서 관리할 수 있음

public interface SampleInterface {
    static public String getName (ID id) {
       //기능 구현이 가능
    }

    default public Sample setDefault(ID id) {
    	return new Person(getName(id), id);
   }   
}

 

* private 메서드

 > 인터페이스에서 private 메서드, private static method를 사용할 수 있음

 > private static  메서드는 다른 static, non-static 메서드 내에서 사용할 수 있음

 > private non-static메서드는 다른 private static 메서드 내에서 사용할 수 없음

public interface SampleInterface {
    static public String getName (ID id) {
       //기능 구현이 가능
    }

    default public Sample setDefault(ID id) {
    	return new Person(getName(id), id);
   }   
   
    private void sample(){
        System.out.println("Hi, Sangguk");
    }
}
728x90

'디자인패턴' 카테고리의 다른 글

Factory Method  (0) 2022.04.25
Singleton  (0) 2022.03.26

댓글