Posts

Showing posts from May, 2013

Java 8: Default Interface Implementations, The Diamond Problem, Multiple Inheritance, and Precedence

Image
If you understand the following cases, you understand Java 8' new interface model . The code below, won't compile throwing this error: java: class FooBar inherits unrelated defaults for someMethod() from types Foo and Bar Java 8: Won't compile Java 8: Compiles The code above compiles and prints: FooBar#main FooBar#someMethod() Foo#someOtherMethod() The code below won't compile as well and throws this familiar (for Java < 8 JDKs) error message: java: FooBar is not abstract and does not override abstract method someMethod() in Foo Java 8: Won't compile As you might see in this examples, starting with JDK 8 , Java has introduced a kind of multiple inheritance as both, the class and its interface might contain an implementation of the same method (same name & signature). To address the diamond problem  there is a precedence in which order an implementation is used: only if the class implements all default / optional methods o