<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Maurice Naftalin&#039;s Lambda FAQ &#187; Default Methods</title>
	<atom:link href="https://www.lambdafaq.org/category/default-methods/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lambdafaq.org</link>
	<description>Your questions answered: all about Lambdas and friends</description>
	<lastBuildDate>Tue, 04 May 2021 15:50:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>What about the diamond problem?</title>
		<link>https://www.lambdafaq.org/what-about-the-diamond-problem/</link>
		<comments>https://www.lambdafaq.org/what-about-the-diamond-problem/#comments</comments>
		<pubDate>Mon, 19 Nov 2012 08:50:20 +0000</pubDate>
		<dc:creator><![CDATA[naftalin]]></dc:creator>
				<category><![CDATA[Default Methods]]></category>

		<guid isPermaLink="false">http://www.lambdafaq.org/?p=619</guid>
		<description><![CDATA[<p>The &#8220;diamond problem&#8221; is an ambiguity that can arise as a consequence of allowing multiple inheritance. It is a serious problem for languages (like C++) that allow for multiple inheritance of state. In Java, however, multiple inheritance is not allowed for classes, only for interfaces, and these do not contain state.</p> <p>Consider the following situation:</p> [...]]]></description>
				<content:encoded><![CDATA[<p>The &#8220;diamond problem&#8221; is an ambiguity that can arise as a consequence of allowing multiple inheritance. It is a serious problem for languages (like C++) that allow for multiple inheritance of state. In Java, however, multiple inheritance is not allowed for classes, only for interfaces, and these do not contain state.</p>
<p>Consider the following situation:</p>
<pre><code>    interface A {
        default void m() { ... }        
    }
    interface B extends A {}
    interface C extends A {}
    class D implements B, C {}
</code></pre>
<p><img src="http://www.lambdafaq.org/wp-content/uploads/Diamond.png" style="border:none; background-color:white; position: absolute; left: 40em; top: 30.5em;" alt="" title="DefaultResolution1" width="47" height="95" class="alignnone size-full wp-image-578"/><br />
The rules for default method selection given on <a href="http://www.lambdafaq.org/how-are-conflicting-method-declarations-resolved/" title="How are conflicting method declarations resolved?">the previous page</a> provide a straightforward interpretation of this scenario and its variants.  </p>
<p>In the initial case (the code above), the implementation of <code>m</code> inherited by <code>D</code> is unambiguously that defined by <code>A</code>&mdash;there is no other possibility.  If the situation is changed so that <code>B</code> now also declares a default implementation of <code>m</code>, that becomes the implementation that <code>D</code> inherits by the &#8220;most specific implementation&#8221; rule.  But if both <code>B</code> and <code>C</code> provide default implementations, then they conflict, and <code>D</code> must provide an overriding declaration, possibly using the syntax <code><em>X</em>.super.<em>m</em>(...)</code> in the body of <code>m</code> to explicitly choose one of the inherited methods. All three cases are clearly covered by the <a href="http://www.lambdafaq.org/how-are-conflicting-method-declarations-resolved/" title="How are conflicting method declarations resolved?">rules for method resolution</a> explained in the previous answer.</p>
<p>Default methods are virtual, like all methods in Java.  This can sometimes lead to surprising results. Given the declarations</p>
<pre><code>    interface A {
        default void m() { System.out.println("hello from A"); }
    }
    interface B extends A {
        default void m() { System.out.println("hello from B"); }
    }
    interface C extends A {}
    class D implements B, C {}
</code></pre>
<p>the code</p>
<pre><code>    C c = new D();
    c.m();
</code></pre>
<p>will print <code>hello from B</code>. The static type of <code>c</code> is unimportant; what counts is that it is an instance of <code>D</code>, whose most specific version of <code>m</code> is inherited from <code>B</code>.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.lambdafaq.org/what-about-the-diamond-problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
