<?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; rgurma</title>
	<atom:link href="https://www.lambdafaq.org/author/rgurma/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>How are conflicting method declarations resolved?</title>
		<link>https://www.lambdafaq.org/how-are-conflicting-method-declarations-resolved/</link>
		<comments>https://www.lambdafaq.org/how-are-conflicting-method-declarations-resolved/#comments</comments>
		<pubDate>Tue, 30 Oct 2012 11:01:42 +0000</pubDate>
		<dc:creator><![CDATA[rgurma]]></dc:creator>
				<category><![CDATA[Default Methods]]></category>

		<guid isPermaLink="false">http://www.lambdafaq.org/?p=537</guid>
		<description><![CDATA[<p>Because inheritance is possible from multiple interfaces, the same default method can be inherited from different paths. Since each inherited default method provides a different implementation, the compiler needs a way of selecting the declaration to use. This is its method:</p> Classes always win. A declaration in the class or a superclass takes priority over [...]]]></description>
				<content:encoded><![CDATA[<p>Because inheritance is possible from multiple interfaces, the same default method can be inherited from different paths. Since each inherited default method provides a different implementation, the compiler needs a way of selecting the declaration to use. This is its method:</p>
<ul>
<li>Classes always win. A declaration in the class or a superclass takes priority over any default method declaration.</li>
<li>Otherwise, the method with the same signature in the <em>most specific</em> default-providing interface is selected. For example, based on the rules above the following code will print &nbsp;<code>Hello World from B</code>:
<pre><code>    public interface A {
        default void hello() { System.out.println("Hello World from A"); }
    }
    public interface B extends A {
        default void hello() { System.out.println("Hello World from B"); }
    }
    public class C implements B, A {
        public static void main(String... args) {
            new C().hello();
        }
    } 
</code></pre>
<p><img src="http://www.lambdafaq.org/wp-content/uploads/DefaultResolution1.png" style="border:none; background-color:white; position: absolute; left: 60em; top: 35em;" alt="" title="DefaultResolution1" width="47" height="95" class="alignnone size-full wp-image-578" />
</li>
</ul>
<p>Conflicts are not always avoidable. If, in the example above, interface <code>B</code> was declared without extending <code>A</code>, then <code>C</code> would inherit default methods with matching signatures from unrelated interfaces. Compiling that code would produce the error messages:</p>
<blockquote style="padding-top:0; padding-bottom:0;"><p>
class C inherits unrelated defaults for hello() from types A and B<br />
reference to hello is ambiguous, both method hello() in A and method hello() in B match.
</p></blockquote>
<p>Note that the first error would be reported whether or not there was a call to the inherited default method. The conflict must be resolved by overriding. If desired, one of the inherited methods can be selected using the new syntax <code><em>X</em>.super.<em>m</em>(...)</code> where <code><em>X</code></em> is the superinterface and <code><em>m</code></em> the method to select:</p>
<pre><code>    public class C implements B, A {
        public void hello() {
            A.super.hello();
        }
        ...
    }
</code></pre>
<p>This now prints &nbsp;<code>Hello World from A</code>. Note that this syntax can only be used to resolve a conflict, not to override either of the two principal rules listed above.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.lambdafaq.org/how-are-conflicting-method-declarations-resolved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
