104

In the Collection Interface I found a method named removeIf() that contains its implementation.

default boolean removeIf(Predicate<? super E> filter) {
    Objects.requireNonNull(filter);  
    boolean removed = false;  
    final Iterator<E> each = iterator();   
    while (each.hasNext()) {  
        if (filter.test(each.next())) {  
            each.remove();  
            removed = true;  
        }  
    }  
    return removed;  
}  

I want to know if there is any way to define method body in an interface?
What is the default keyword and how does it work?

user2864740
  • 60,010
  • 15
  • 145
  • 220
gifpif
  • 4,507
  • 4
  • 31
  • 45
  • 3
    see this post about the default http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/#!/ – emeraldjava Aug 17 '13 at 07:23
  • Related post https://stackoverflow.com/questions/31578427/what-is-the-purpose-of-the-default-keyword-in-java – Ravi Oct 08 '17 at 08:21

3 Answers3

185

From https://dzone.com/articles/interface-default-methods-java

Java 8 introduces “Default Method” or (Defender methods) new feature, which allows developer to add new methods to the interfaces without breaking the existing implementation of these interface. It provides flexibility to allow interface define implementation which will use as default in the situation where a concrete class fails to provide an implementation for that method.

public interface A {
    default void foo(){
       System.out.println("Calling A.foo()");
    }
}

public class ClassAB implements A {
}

There is one common question that people ask about default methods when they hear about the new feature for the first time:

What if the class implements two interfaces and both those interfaces define a default method with the same signature?

Example to illustrate this situation:

public interface A {  
    default void foo(){  
        System.out.println("Calling A.foo()");  
    }  
}

public interface B {
    default void foo(){
        System.out.println("Calling B.foo()");
    }
}


public class ClassAB implements A, B {

}  

This code fails to compile with the following result:

java: class Clazz inherits unrelated defaults for foo() from types A and B

To fix that, in Clazz, we have to resolve it manually by overriding the conflicting method:

public class Clazz implements A, B {
    public void foo(){}
}

But what if we would like to call the default implementation of method foo() from interface A instead of implementing our own.

It is possible to refer to A#foo() as follows:

public class Clazz implements A, B {
    public void foo(){
       A.super.foo();
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
gifpif
  • 4,507
  • 4
  • 31
  • 45
51

Those methods are called default methods. Default method or Defender method is one of the newly added features in Java 8.

They will be used to allow an interface method to provide an implementation used as default in the event that a concrete class doesn't provide an implementation for that method.

So, if you have an interface, with a default method:

public interface Hello {
    default void sayHello() {
        System.out.println("Hello");
    }
}

The following class is perfectly valid:

public class HelloImpl implements Hello {

}

If you create an instance of HelloImpl:

Hello hello = new HelloImpl();
hello.sayHello();  // This will invoke the default method in interface

Useful Links:

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • So it is ok if a class implements an interface and not implement it's method? As far as Java7 is concerned which I am using this is not allowed. – Aniket Thakur Aug 17 '13 at 07:28
  • 2
    @AniketThakur. This is not allowed before Java 8. This feature is added in Java 8 only. You can avoid giving the implementation of *default* methods in your implementing class. – Rohit Jain Aug 17 '13 at 07:29
  • Great then +1 for the answer :) – Aniket Thakur Aug 17 '13 at 07:30
  • if i use default implementation in interface than is it necessary to give implementation in its implemented class . – gifpif Aug 17 '13 at 07:35
  • 1
    @PawanMishra. See my previous comment. No you don't need to provide implementation of default interface methods in implementing class. – Rohit Jain Aug 17 '13 at 07:36
  • 1
    @PawanMishra you can override it though. There is no restriction such as you need to use default implementation only. – Aniket Thakur Aug 17 '13 at 07:41
  • 4
    A forward step that will finally avoid being puzzled by multiple inheritance! – Aritz Mar 22 '14 at 13:15
20

I did a bit of research and i found the following. Hope this helps.

Existing problem

Normal interface methods are declared as abstract and must be defined in the class that implements the interface. This 'burdens' the class implementer with the responsibility to implement every declared method. More importantly, this also means that extending an interface is not possible after 'publication'. Otherwise, all implementers would have to adapt their implementation, breaking backwards source and binary compatibility.

Solution adopted in Java 8

To cope with these problems, one of the new features of JDK 8 is the possibility to extend existing interfaces with default methods. Default methods are not only declared, but also defined in the interface.

Important points to note

  1. Implementers can choose not to implement default methods in implementing class.
  2. Implementers can still override default methods, like regular non-final class methods can be overridden in subclasses.
  3. Abstract classes can even (re)declare default methods as abstract, forcing subclasses to reimplement the method (sometimes called 're-abstraction').
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289