Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

How does Java 8' new default interface model works (incl. diamond, multiple inheritance, and precedence)?

How does this new interface model works and what is about

  • the diamond problem that might arise out of this
  • multiple inheritance character of this implementation
  • and the precedence with which the interface implementations are used ?
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a perfect explanation at Java Lambda FAQ.
Here is a citation from What about the diamond problem? article there:

interface A {
    default void m() { ... }        
}
interface B extends A {}
interface C extends A {}
class D implements B, C {}

In the initial case (the code above), the implementation of m inherited by D is unambiguously that defined by A — there is no other possibility. If the situation is changed so that B now also declares a default implementation of m, that becomes the implementation that D inherits by the “most specific implementation” rule. But if both B and C provide default implementations, then they conflict, and D must either use the syntax X.super.m(...) to explicitly choose one of them, or else redeclare the method itself, overriding all supertype declarations.

Be sure to check out previous article on rules of resolving conflicting method declarations and other articles on Java Lambda project — they are quite good.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...