Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

A few days ago, a student asked me why a default method was added to the Java interface. Java 8 adds not only default interface methods but also static interface methods. Let’s talk about these two methods today.

Default interface methods

The default interface methods in Java are defined like this:

 public interface NewInterface {
 ​
     void otherMethod();
     
     default void doSomething() {
         System.out.println(" do something ");
     }
 }
 ​
 public class NewInterfaceImpl implements NewInterface {
     @Override
     public void otherMethod() {
 ​
     }
 }
Copy the code

Use the default keyword declaration in the interface and provide an implementation, and the method can be called publicly without adding the public keyword, even if you can Override it in the implementation class (@override).

In abstract-based conceptual design, abstractions often have multiple implementations, and it is not necessary to abstract if there are no multiple implementations. The implementation of an interface must implement all of its abstract methods. This is not an optional behavior. If we add a new method to an interface, it will affect all of its implementation classes.

The interface default approach is an effective way to solve this problem. It allows a feature implementation to be added directly to the interface, and all implementation classes can use this feature implementation without modifying the implementation classes. In this way, backward compatibility can be subtly preserved. It is also the only way to extend a functional interface, and without a default method, it can cause a “catastrophic” chain reaction if the functional interface needs to be extended.

So the default interface methods are a really nice feature, but there are a few issues that need to be addressed. Since Java allows classes to implement multiple interfaces, what happens when a class implements multiple interfaces that define the same default method? Let’s define an interface with a default interface method named doSomething:

public interface AnotherInterface { void anotherMethod(); default void doSomething() { System.out.println(" do another something "); }}Copy the code

If you have a class that implements both NewInterface and AnotherInterface

Because the method signatures are the same, there is no way to determine which method to use or even compile it. To resolve this disagreement, we must explicitly provide an implementation for these methods.

Static interface method

Starting with Java 8, we can also define static methods in interfaces. Since static interface methods do not belong to a specific object, they are not part of the interface implementation class. You cannot call static interface methods from the implementation class, only from the interface. Using this feature, we can implement some fixed paradigm functions that do not change due to interface polymorphism, avoiding adverse consequences.

@FunctionalInterface public interface Customizer<T> { void customize(T t); static <T> Customizer<T> withDefaults() { return (t) -> { }; }}Copy the code

This is the key interface to Spring Security’s custom configuration. If you use the default configuration you can implement it statically, and if you want to customize it you can implement the abstract interface. This way the uniform paradigm can be stabilized, usually as an aid to functionality, and this approach cannot be overridden.

The scene difference between the two

  • The default interface methods provide default implementations of functionality that you can change if you don’t like.
  • Static interface methods also provide a default implementation of functionality, sorry to use it.