<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: FIROUD-Reda</title>
    <description>The latest articles on DEV Community by FIROUD-Reda (@akihiki).</description>
    <link>https://dev.to/akihiki</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1160975%2F6fe6a321-32c7-4e81-a101-7afcd7bb4c67.jpg</url>
      <title>DEV Community: FIROUD-Reda</title>
      <link>https://dev.to/akihiki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akihiki"/>
    <language>en</language>
    <item>
      <title>Java Interfaces Default Methods</title>
      <dc:creator>FIROUD-Reda</dc:creator>
      <pubDate>Thu, 14 Sep 2023 09:14:07 +0000</pubDate>
      <link>https://dev.to/akihiki/java-interfaces-default-methods-ge</link>
      <guid>https://dev.to/akihiki/java-interfaces-default-methods-ge</guid>
      <description>&lt;p&gt;In an application where an interface has one or multiple implementations, if we add a method to the interface, all of its implementations will be forced to implement it&lt;/p&gt;

&lt;p&gt;But using default interface methods, we can add new methods to an interface that are automatically available in the implementations. Therefore, there’s no need to modify the implementing classes.&lt;/p&gt;

&lt;p&gt;Let’s see an example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3p8nrf3y526utcyu8rla.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3p8nrf3y526utcyu8rla.png" alt="Image description" width="568" height="551"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we have our Animal interface containing two default methods, "sleep" and "wake up." Now, let's implement it in two classes, "Cat" and "Bird."&lt;/p&gt;

&lt;p&gt;Cat class: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpixf6njxlnkbx55vjzj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpixf6njxlnkbx55vjzj8.png" alt="Image description" width="506" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bird class:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv9ntyd50cblyu1j9kxyc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv9ntyd50cblyu1j9kxyc.png" alt="Image description" width="584" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, as you can see, we did not implement either of the two default methods in the “Cat” and “Bird” classes. However, as we can observe in our main method, these default methods are available in both classes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1of2l2fqf3lsus3lkg9m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1of2l2fqf3lsus3lkg9m.png" alt="Image description" width="619" height="624"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far so good . However, as you might have already guessed, this could lead to a big problem if we implement multiple interfaces with the same default method (the multiple inheritance dilemma v2.0 😅).&lt;/p&gt;

&lt;p&gt;But don’t worry, the Java team has you covered. Let’s see an example. We’ll add a new interface called “Mammal” and implement it in our “Cat” class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa2d3vq1kvk9hf79h3g3u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa2d3vq1kvk9hf79h3g3u.png" alt="Image description" width="766" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that our Cat class implements both the Mammal and Animal interfaces, it might get confused about which default method to use, and our application will raise the following error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8svh5bxrhe368ltknrff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8svh5bxrhe368ltknrff.png" alt="Image description" width="800" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To fix this error, we should override the method and either create a new implementation of our own. In this implementation, if we want, we can basically call one of the old implementations explicitly using the super keyword. This allows us to specify which implementation to use and resolve the ambiguity.&lt;/p&gt;

&lt;p&gt;Case 1 : Simple new implementation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6msxkyvketabsc8406f5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6msxkyvketabsc8406f5.png" alt="Image description" width="743" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Case 2 : as you may see I called both implementations to show you that it is possible , or you may choose the one that suits you&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhe5ov0ro98xlryns6li3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhe5ov0ro98xlryns6li3.png" alt="Image description" width="457" height="618"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you now have a clear understanding of what default methods are, and thank you for reading.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Akihiki&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>java</category>
      <category>development</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
