On the default and static methods of Java

On the default and static methods of Java

It is allowed to declare default methods and static methods in the interface, which is jdk1 8 new features. Existence is reasonable. The appearance of the two makes the interface more and more like an abstract class (the next article summarizes the difference between the two). Then why do they appear and what convenience they bring? Java Xiaobai starts to learn and summarize. For the shortcomings, please give some advice in the comment area!

What's the use of adding default methods in Java

Official answer: the default method allows you to add new functions to the interface of the existing library and ensure binary compatibility with the code written with the interface of the old version. This boring introduction seems difficult to understand. Take a simple example. Suppose there is a big project, an interface is implemented by many classes, and everyone is running smoothly. Suddenly one day, there was a small problem, or a better optimization scheme, which needed to be added in these implementation classes. Before the default method appeared, only abstract methods and specific definitions need to be given in the implementation class to operate. Wouldn't it be that you can only close your eyes and add them directly from morning to night. However, the appearance of the default method allows the specific implementation of the method to be given 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 my example is inappropriate, please correct it.

package com.my.pac21;

/**
 * @auther Summerday
 */

interface Closable {
    void close();
    //假设是新增的默认方法
    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");
    }

    //两个接口中包含同名的方法,需要重写,指定一个
    @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

        //实现默认方法
        cw.makeSound();//peng!
        cd.makeSound();//need to override default methods
    }
}

What's the use of the new static methods in Java

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 the static methods. Start frantically searching for information...

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 the interface. The appearance of static methods in the interface can directly call static methods through the interface.

package com.my.pac21;

/**
 * @auther Summerday
 */
public class Test {
    public static void main(String[] args) {
        int val1 = 5;
        int val2 = 6;
        //通过创建实现类的对象
        Countable b = new CountableCompanion();
        System.out.println(b.getNum(val1,val2));
        //直接通过接口调用
        Countable.getMul(val1,val2);
    }
}
interface Countable{
    //普通抽象方法
    int getNum(int a,int b);
    //静态方法
    static int getMul(int a,int b){
        return a*b;
    }
}
//实现接口的实现类
class CountableCompanion implements Countable{
    @Override
    public int getNum(int a,int b) {
        return a+b;
    }
}

This is an example that I think is quite childish for understanding only.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>