/java/

Java8 - Default Methods for Interfaces

2014-11-02 02:17:58

Document.java

interface Document {

    void edit();

    default void document() {
        System.out.println("document");
    }
}

Cover.java

public interface Cover {

    default void cover(){
        System.out.println("cover");
    }

}

Book.java

public class Book implements Document, Cover {
    @Override
    public void edit() {
        System.out.println("edit book");
    }
}

Main.java

public class Main {

    public static void main(String[] args) {

        Document document = new Document() {
            @Override
            public void edit() {
                System.out.println("edit document");
            }
        };

        document.edit();
        document.document();

        System.out.println("-------------");

        Book book = new Book();
        book.edit();
        book.cover();
        book.document();
    }

}