Java-based interface composition update

Get into the habit of writing together! This is the 9th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event

1.1 Overview of Interface Composition Update

  • The composition of the interface
    • constant:public static final
    • Abstract method:public abstract
    • Default method (Java 8)
    • Static methods (Java 8)
    • Private methods (Java 9)

1.2 Default methods in interfaces (JDK8)

We all know that when a class implements an interface, it is necessary to override all abstract methods in the interface. But now a new method has been added to the interface, what if the class that implements this interface does not want to override this method? At this time, the default method in the interface can be used, it is not forced to be overridden, and the method body can also be provided.

  • The definition format of the default method in the interface:
    • Format:public default 返回值类型 方法名(参数列表){}
    • example:public default void show(){}
  • Notes on default methods in interfaces:
    • Default methods are not abstract methods, so are not forced to be overridden. But it can be overridden, remove the default keyword when overriding
    • public can be omitted, default cannot be omitted:default void show(){}

1.3 Static methods in interfaces (JDK8)

  • The definition format of static method in interface:

    • Format:public static 返回值类型 方法名(参数列表){}
    • example:public static void show(){}
  • Notes on static methods in interfaces:

    • Static methods can only be called by interface name , not by implementation class name or object name
    • public can be omitted, static cannot be omitted:static void show(){}
  • interface

package test;

public interface Inter {
    void show();

    default void method() {
        System.out.println("默认方法");
    }

//    public static void test(){
//        System.out.println("静态方法");
//    }

    static void test(){
        System.out.println("静态方法");
    }
}
复制代码
  • Implementation class
package test;

public class InterImpl implements Inter{
    @Override
    public void show() {
        System.out.println("show方法");
    }
}
复制代码
  • test class
package test;

public class Demo {
    public static void main(String[] args) {
        Inter i = new InterImpl();
        i.show(); //show方法
        i.method(); //
//        i.test(); //报错

        Inter.test(); //静态方法,接口名调用静态方法
    }
}
复制代码

1.4 Private methods in interfaces (JDK9)

In Java 9, private methods with method bodies were added, which actually laid the groundwork in Java 8: Java 8 allows default methods and static methods with method bodies to be defined in interfaces. This may cause a problem: when two default methods or static methods contain the same code implementation, the program must consider extracting this implementation code into a common method, and this common method does not need to be used by others , so it is hidden with private, which is the inevitability of adding private methods in Java 9.

  • The definition format of private methods in an interface :
    • Format 1 (non-static) :private 返回值类型 方法名(参数列表){}
    • Example 1:private void show() {}
    • Format 2 (static) :private static 返回值类型 方法名(参数列表){}
    • Example 2:private static void method() {}
  • Notes on private methods in interfaces :
    • Default methods can call private static and non-static methods
    • Static methods can only call private static methods
package test;

public interface Inter {
    default void show1() {
        System.out.println("show1开始执行");
//        System.out.println("初级工程师");
//        System.out.println("中级工程师");
//        System.out.println("高级工程师");
//        show();
        method();
        System.out.println("show1结束");
    }


    static void method1() {
        System.out.println("method1开始执行");
//        System.out.println("初级工程师");
//        System.out.println("中级工程师");
//        System.out.println("高级工程师");
        method();
        System.out.println("method1结束");
    }

    private void show(){
        System.out.println("初级工程师");
        System.out.println("中级工程师");
        System.out.println("高级工程师");
    }

    private static void method(){
        System.out.println("初级工程师");
        System.out.println("中级工程师");
        System.out.println("高级工程师");
    }
}
复制代码

Guess you like

Origin juejin.im/post/7084937783864721444