0

Java 8 allows us to add non-abstract method implementations to interfaces by utilizing the default keyword. For example:

interface Foo {
    Object method1();

    default Object method2() {
        return new Object();
    }
}

What is the reason for this? Isn't it just making an interface look more like an abstract class? Or there is something else I am missing?

Jadiel de Armas
  • 8,405
  • 7
  • 46
  • 62
  • http://stackoverflow.com/questions/19998309/purpose-of-default-or-defender-methods-in-java-8?rq=1 – GriffeyDog Mar 05 '15 at 20:03
  • 3
    This has been discussed over and over again. The main point is to be able to add new methods to existing interfaces (collections, mainly), without breaking all the already existing implementations of these interfaces. list.stream() will work even for a List implementation written 5 years ago. – JB Nizet Mar 05 '15 at 20:04
  • Can I overwrite a default method? – Jadiel de Armas Mar 05 '15 at 20:05
  • @JadieldeArmas Default methods are well-documented. Have you looked? – GriffeyDog Mar 05 '15 at 20:07
  • Thanks for pointing that.I just found the link. Yes, you can. http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html – Jadiel de Armas Mar 05 '15 at 20:09

0 Answers0