Java 8 default methods

Starting from Java 8, a method which is defined in an interface can have a default implementation:

For example:

interface Descriptive {
    default String desc() {
        return "fantastic";
    }
}
 
class Item implements Descriptive {
    // the method "desc" is not implemented here
}
 
Item item = new Item();
item.desc(); // "fantastic"
 
class Book implements Descriptive {
    @Override
    public String desc() {
        return "Cool book!";
    }
}
 
Book book = new Book();
book.desc(); // "Cool book!"
Using default methods in interfaces

Subscribe to Java 8 Resources

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe