What is the "default" implementation of a method defined in an interface?

In the collection interface, I found a method called 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 a way to define the body of a method in an interface?
What is the default keyword and how does it work?

+90
java java-8 interface
Aug 17 '13 at 7:21
source share
3 answers

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

Java 8 introduces a new feature called β€œDefault Method” or (Defender Methods), which allows a developer to add new methods to interfaces without breaking existing implementation of these interfaces. This provides flexibility to allow the implementation of an interface definition that will be used by default in situations where a particular class cannot provide an implementation for this 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 first hear about a new feature:

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

An 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 does not compile with the following result:

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

To fix this, in Clazz we need to resolve this 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 the foo () method from interface A instead of implementing our own.

A # foo () can be accessed as follows:

 public class Clazz implements A, B { public void foo(){ A.super.foo(); } } 
+162
Aug 17 '13 at 8:57
source share

These methods are called default methods. The default method or Defender method is one of the recently added features in Java 8.

They will be used to allow an interface method to provide a default implementation if a particular class does not provide an implementation for this method.

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

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

The following class is quite true:

 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:

+49
Aug 17 '13 at 7:24 on
source share

I did a little work and found the following. Hope this helps.

Existing problem

Normal interface methods are declared abstract and must be defined in the class that implements the interface. This "burdens" the executing class with responsibility for the execution of each declared method. More importantly, it also means that extending the interface is not possible after publication. Otherwise, all developers would have to adapt their implementation, violating the source and binary compatibility.

Decision made in Java 8

To deal with these issues, one of the new features in JDK 8 is the ability to extend existing interfaces using default methods. The default methods are not only declared, but also defined in the interface.

It is important to note

  • Artists may not use the default methods in the implementing class.
  • Executors can still override standard methods, such as regular methods of a non-finite class, can be overridden in subclasses.
  • Abstract classes can even (re) declare default methods as abstract, forcing subclasses to override a method (sometimes called "re-abstraction").
+17
Aug 17 '13 at 7:46
source share



All Articles