default메서드 & static 메서드

coding by 스플릿·2022년 1월 2일
0

Java1

목록 보기
38/44

존재 이유

  • 인터페이스에 메서드를 추가하는 경우는 인터페이스를 구현하는 모든 클래스들이 추가된 매서드를 구현해야 하기 때문에 매우 해야할 일이 늘어나기 때문에

default 메서드

  • 작성 : public default void methos1(){}
  • 추상메서드 대신 default 메서드를 선언하면 인터페이스를 구현한 클래스들이 추상메서드처럼 구현을 할 필요하가 없다.

사용 예시

class Main{
    public static void main(String[] args) {
        Myinterface.static_method();
        child c = new child();
        c.default_method();
    }
}
interface Myinterface{
    static void static_method(){
        System.out.println("Myinterface static method");
    }
    default void default_method(){
        System.out.println("Default method");
    }
}
class child implements Myinterface{
}

0개의 댓글