Java 8 Default Methods in Interface

Java Interface always can contain the method declaration and cannot contain method implementation (method body), “there is no possible way of adding method implementation to a Java Interface” has become a myth now after the introduction of Default Method in Java 8. Default Method or Defender methods is a new feature which lets the developer add a new method to an existing interface and provides the implementation for it without breaking existing implementation of the interface. For defining method body, we simply need to add Java default keyword while declaring the method. Let’s look into the below example and understand how Default Methods works.

package com.javainterviewpoint;

public interface Test
{
    public void method1();
    // add default keyword to provide implementation
    default public void method2()
    {
        System.out.println("Implementation of Default Methods");
    }
}
  • In the Test interface, we have two methods, method1() is an abstract method and doesn’t have any implementation for it.
  • method2() has the implementation, we simply need to add java default keyword to make it a default method.
package com.javainterviewpoint;

public class TestImpl implements Test
{
    @Override
    public void method1()
    {
        System.out.println("Method1() implementation");
    }
    
    public static void main(String[] args)
    {
        //Create object for TestImpl class
        TestImpl testImpl = new TestImpl();
        //Calling method1() implementation
        testImpl.method1();
        //Calling method2() implemented in Test Interface
        testImpl.method2();
    }
}

TestImpl class implements the Test interface, hence we need to override method1() and provide the implementation for it. Since method2() is already implemented as a Default Method in Test Interface we don’t need to give implementation for it again.

Output :
On Execution of TestImpl class, we will be getting the below output

Default Methods in Java 8

Why do we need Default Methods / Virtual Extension Methods ?

Why do we need Default Methods in Java ? will be a question here again. Traditional Interfaces are tightly coupled with the class which is implementing it. It is not possible to add a method to an interface without breaking the chain of classes which is implementing the interface. Even then all the classes need to provide a body for the newly added method.

Default method were introduced in Java 8 for providing backward compatibility. A method can be added to an interface without affecting implementing classes. Even if a new method is added to the interface and the method has body, No implementation classes will be affected by this and the implementation class can also override the newly added method.

In JDK 8, itself we can see the implementation of Default Method. We all know in Java 8 ForEach method has been introduced to iterate a collection. The forEach() method is a Defender method, we can see its implementation in the Iterable interface.

public interface Iterable
{
    public default void forEach(Consumer<? super T> consumer)
    {
        for (T t : this)
        {
            consumer.accept(t);
        }
    }
}

Java Interface Static method

In Java 8, the answer to questions like, can we declare static method in interface ? or can interface have static methods ? is YES. In addition to adding an instance methods to an interface, we can also be able to add static methods.  We just need to add static keyword to the method signature and provide method body. It acts as the helper method, and can be called with interface name itself.

Test.java

package com.javainterviewpoint;

public interface Test
{
    public void method1();
    
    default public void method2()
    {
        System.out.println("Implementation of Default Methods");
        //calling the static method
        System.out.println(Test.method3());
    }
    
    static String method3()
    {
        return "Static method in Interface";
    } 
}

Here static method method3() act as a helper method for the Test interface. It can be simply called with interface name.method name.

TestImpl.java

package com.javainterviewpoint;

public class TestImpl implements Test
{
    @Override
    public void method1()
    {
        System.out.println("Method1() implementation");
    }
    
    public static void main(String[] args)
    {
        //Create object for TestImpl class
        TestImpl testImpl = new TestImpl();
        //Calling method1() implementation
        testImpl.method1();
        //Calling method2() implemented in Test Interface
        testImpl.method2();
        
        testImpl.method3();
        
    }
    public void method3()
    {
        System.out.println("method 3() "+Test.method3());
    }
}

Output :

Method1() implementation
Implementation of Default Methods
Static method in Interface
method 3() Static method in Interface
Note : static method cannot be overridden in the TestImpl class. If you do so you will be getting error like “The method method3() of type TestImpl must override or implement a supertype method”

Difference between Default Methods and Abstract class

After the introduction of Default Method in Java 8, it seems that interfaces and abstract classes are same. However, they are still different concept in Java. Abstract class can hold state of the object. It can have constructors and member variables. Whereas interfaces with Java 8 default methods cannot hold state. Interface cannot have constructors and member variables as well.

In general you need to use the abstract class when you need to hold the state and when you need constructor and Use Default Methods when you are looking for backward compatibility or add additional functionality to a existing interface without breaking the chain of the implementation classes.

Leave a Reply

Your email address will not be published. Required fields are marked *