It is possible to write default methods and static methods in the interface after JDK8. If you are not sure, come in and have a look!!!

Recently, I just have time to sort out the features of JDK8 for everyone. This role in actual development is getting more and more important. This article focuses on the changes in the interface.

JDK8 interface changes

1. New interface in JDK8

There are enhancements to the interface in JDK8, before JDK8

interface 接口名{
    静态常量;
    抽象方法;
}

After JDK8, the interface has been added, and the interface can have default methods and static methods

interface 接口名{
    静态常量;
    抽象方法;
    默认方法;
    静态方法;
}

2. The default method

2.1 Why add a default method

Before JDK8, there can only be abstract methods and static constants in the interface, and there will be the following problems:

If an abstract method is added to the interface, the implementation class must abstract this abstract method, which is very unfavorable for the expansion of the interface

package com.bobo.jdk.inter;

public class Demo01Interface {

    public static void main(String[] args) {
        A a = new B();
        A c = new C();
    }
}

interface A{
    void test1();
    // 接口中新增抽象方法,所有实现类都需要重写这个方法,不利于接口的扩展
    void test2();
}

class B implements  A{
    @Override
    public void test1() {

    }

    @Override
    public void test2() {

    }
}

class C implements A{
    @Override
    public void test1() {

    }

    @Override
    public void test2() {

    }
}

2.2 The format of the interface default method

The syntax format of the default method in the interface is

interface 接口名{
    修饰符 default 返回值类型 方法名{
        方法体;
    }
}
package com.bobo.jdk.inter;

public class Demo01Interface {

    public static void main(String[] args) {
        A a = new B();
        a.test3();
        A c = new C();
        c.test3();

    }
}

interface A{
    void test1();
    // 接口中新增抽象方法,所有实现类都需要重写这个方法,不利于接口的扩展
    void test2();

    /**
     * 接口中定义的默认方法
     * @return
     */
    public default String  test3(){
        System.out.println("接口中的默认方法执行了...");
        return "hello";
    }
}

class B implements  A{
    @Override
    public void test1() {

    }

    @Override
    public void test2() {

    }

    @Override
    public String test3() {
        System.out.println("B 实现类中重写了默认方法...");
        return "ok ...";
    }
}

class C implements A{
    @Override
    public void test1() {

    }

    @Override
    public void test2() {

    }
}

2.3 The use of default methods in the interface

There are two ways to use the default method in the interface

  1. The implementation class directly calls the default method of the interface
  2. Implement the default method of the class override interface

3. Static methods

A static method is added to the interface in JDK8, which is also used for the expansion of the interface

3.1 Grammar rules

interface 接口名{
    修饰符 static 返回值类型 方法名{
        方法体;
    }
}
package com.bobo.jdk.inter;

public class Demo01Interface {

    public static void main(String[] args) {
        A a = new B();
        a.test3();
        A c = new C();
        c.test3();
        A.test4();
    }
}

interface A{
    void test1();
    // 接口中新增抽象方法,所有实现类都需要重写这个方法,不利于接口的扩展
    void test2();

    /**
     * 接口中定义的默认方法
     * @return
     */
    public default String  test3(){
        System.out.println("接口中的默认方法执行了...");
        return "hello";
    }

    /**
     * 接口中的静态方法
     * @return
     */
    public static String test4(){
        System.out.println("接口中的静态方法....");
        return "Hello";
    }
}

class B implements  A{
    @Override
    public void test1() {

    }

    @Override
    public void test2() {

    }

    @Override
    public String test3() {
        System.out.println("B 实现类中重写了默认方法...");
        return "ok ...";
    }

}

class C implements A{
    @Override
    public void test1() {

    }

    @Override
    public void test2() {

    }
}

3.2 Use of static methods

The static method in the interface cannot be overridden in the implementation class, and the call can only be realized by the interface type: interface name. static method name ();

Insert picture description here

4. Introduction to the difference between the two

  1. The default method is called by the instance, the static method is called by the interface name
  2. The default method can be inherited, the implementation class can directly call the interface default method, or override the interface default method
  3. Static methods cannot be inherited, and the implementation class cannot override the static methods of the interface, and can only be called using the interface name