Simple understanding of Java’s default and static methods

Time:2020-11-27

This article mainly introduces a simple understanding of Java default and static methods, the article through the example code is very detailed, for everyone’s study or work has a certain reference learning value, need friends can refer to

On the default and static methods of Java

Allowing default and static methods to be declared in interfaces is a new feature of JDK1.8. Existence is reasonable. The appearance of the two makes the interface more and more like abstract classes. Why do they appear? What are the conveniences brought about by their appearance? Java Xiaobai starts to learn and summarize. What are the shortcomings? Please point out one or two in the comment area!

What’s the use of adding default methods in Java

Official answer: the default method allows you to add new features to the interface of an existing library and ensure binary compatibility with code written with older versions of the interface.
It seems difficult to understand this boring introduction. Let me give you a simple example. Suppose there is a very large project, an interface is implemented by many classes, and everyone is running smoothly. Suddenly one day, there is a small problem, or there is a better optimization scheme, which needs to be added in these implementation classes. Before the emergence of default methods, only abstract methods can be operated, and specific definitions need to be given in the implementation class. It’s not only a matter of two eyes, but also can be added directly from morning to night.

However, the appearance of the default method allows the specific implementation of the method in the interface, and the default method can be automatically implemented in the implementation class. I only need to put this optimization in the default method of the interface to complete the optimization of all implementation classes. Of course, it is purely personal understanding. If there is something wrong with my example, please correct it.

package com.my.pac21;

/**
 * @auther Summerday
 */

interface Closable {
  void close();
  //Assumption is the new default method
  default void makeSound() {
    System.out.println("peng!");
  }
}

interface Openable {
  default void makeSound() {
    System.out.println("peng!");
  }
}

class Window implements Closable {

  @Override
  public void close() {
    System.out.println("Window.close");
  }
}

public class Door implements Closable, Openable {

  @Override
  public void close() {
    System.out.println("Door.close");
  }

  //The two interfaces contain methods with the same name, which need to be overridden to specify one
  @Override
  public void makeSound() {
    System.out.println("need to override default methods");
  }

  public static void main(String[] args) {
    Closable cw = new Window();
    Closable cd = new Door();
    cw.close();//Window.close
    cd.close();//Door.close

    //Implement default method
    cw.makeSound();//peng!
    cd.makeSound();//need to override default methods
  }
}

What’s the use of Java’s new static methods

The appearance of default methods and static methods in the interface makes the interface lose the feature of “all abstract methods”. After exploring the new default methods, we should start with static methods. Start searching for information…

Before Java 8 made it possible to declare static methods in interfaces, it was common practice to place these methods in companion utility classes. For example, the java.util.Collections class is a companion to the java.util.Collection interface, and declares static methods that would be more appropriate in the relevant Java Collections Framework interfaces. You no longer need to provide your own companion utility classes. Instead, you can place static methods in the appropriate interfaces, which is a good habit to cultivate.

This is the answer I found on stack overflow. What does it mean? Before adding static methods, if we want some fixed operations to appear in the interface, we must define an implementation class matching with the interface. The appearance of static method in interface can call static method directly through interface.

package com.my.pac21;

/**
 * @auther Summerday
 */
public class Test {
  public static void main(String[] args) {
    int val1 = 5;
    int val2 = 6;
    //By creating objects that implement classes
    Countable b = new CountableCompanion();
    System.out.println(b.getNum(val1, val2));
    //Call directly through the interface
    Countable.getMul(val1,val2);
  }
}
interface Countable{
  //General abstract method
  int getNum(int a,int b);
  //Static method
  static int getMul(int a,int b){
    return a*b;
  }
}
//The implementation class that implements the interface
class CountableCompanion implements Countable{
  @Override
  public int getNum(int a,int b) {
    return a+b;
  }
}

This is an example that I think is still relatively naive, just for understanding.

Common abstract method: I define an abstract method in the interface, and then I define the implementation class to implement the method. Finally, I call the method by creating an instance of the implementation class, and finally calculate the sum of the two values. As you can imagine, in practice, if methods of the same nature want to be implemented in multiple implementation classes, this method is more troublesome.

In the case of static methods: the static methods are defined directly in the interface and can be called directly by the interface. It is not necessary to define the corresponding implementation class. How comfortable it is.

All of the above, I hope that all of the above will be helpful to you.