Code Pumpkin

Default Method In Interface | Java8

May 13, 2017
Posted by Dipen Adroja

In this article, we will discuss Default Method in interface which was introduced from Java8. This feature allows developer to add new methods to the interfaces without breaking the existing implementation of these interface. It provides flexibility to allow interface to define implementation which will will be used when concrete class does not provide an implementation for that method.

Re-engineering an existing JDK framework is always very complex. Modify one interface in JDK framework breaks all classes that extends the interface which means that adding any new method could break millions of lines of code. This capability is added for backward compatibility so that old interfaces can be used to leverage the lambda expression capability of Java 8. For example, ‘List’ or ‘Collection’ interfaces do not have ‘forEach’ method declaration. Thus, adding such method will simply break the collection framework implementations. Java 8 introduces default method so that List/Collection interface can have a default implementation of forEach method, and the class implementing these interfaces need not to implement the same.

We can have more than one default methods in our interface. We can also declare static method as well. Default method can be overridden in implementation class like other normal interface methods. If we want to restrict the overriding of the methods, we can use static methods. Below is the example of default and static method in interface.

Example:


interface CodePumpkinInt
{
    // Constants and other abstract and default methods
     
    static void staticMethodOne()
    {
        //common code
        System.out.println("First Static Method");
    }
    default void defaultMethodOne()
    {
        //common code
        System.out.println("First Default Method");
    }
}

public class CodePumpkinImpl implements CodePumpkinInt{

	public static void main(String[] args){
		CodePumpkinImpl codePumpkinImpl = new CodePumpkinImpl ();
		// print “First Default Method”
		codePumpkinImpl.defaultMethodOne();
	}
}

Interface with default Method and Abstract class:

Now if we compare interface with default method and Abstract class it is almost same. But still there is a difference. Interface can not have constructor. So abstract class is still more structured then interface and can hold a state. While in contrast, default method can be implemented only in the terms of invoking other interface methods, with no reference to a particular implementation's state. Hence, both use for different purposes and choosing between two really depends on the scenario context.

Default Method and Multiple Inheritance:

Now to understand the issue with multiple inheritance with default method of the same name and signature, check the below example.


public interface InterfaceA { 
    default void defaultMethod(){ 
        System.out.println("Interface A default method"); 
    } 
}

public interface InterfaceB {
    default void defaultMethod(){
        System.out.println("Interface B default method");
    }
}

public class Impl implements InterfaceA, InterfaceB  {

}

The above code will not compile, because InterfaceA and InterfaceB both have the default method. So Impl class can not decide which defaultMethod to call. To resolve this issue we need to override the defaultMethod in Impl class and then manually call the method we want to be called. Below code snippet will solve the above issue of multiple inheritance.


public class Impl implements InterfaceA, InterfaceB {
 
   public void defaultMethod(){
        // existing code here..
        InterfaceA.super.defaultMethod();
    }

}

In Java9, Oracle has introduced private methods as well. To know more about it, we can check this article on Private Methods Interface Java9.

To go through in more details of the Default Method, we can always refer to Oracle Doc. To know more about other Java8 features you can go through this post.

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.



About the Author


Coder, Blogger, Wanderer, Philosopher, Curious pumpkin



Tags: , , ,


Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
</code></pre>
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 124
follow us in feedly

Like Us On Facebook