Home  >  Article  >  Java  >  How to implement Java interface composition update

How to implement Java interface composition update

WBOY
WBOYforward
2023-05-11 22:58:041083browse

1.1 Overview of interface composition update

  • Composition of interface

    • Constant: public static final

    • Abstract method: public abstract

    • ##Default method (Java 8)

    • Static method (Java 8)

    • Private method (Java 9)

1.2 Interface Default method (JDK8)

We all know that if a class implements an interface, it must rewrite all abstract methods in this interface. But now there is a new method in the interface. What should I do if the class that implements this interface does not want to override this method? At this time, you can use the default method in the interface, which is not forced to be overridden, and can also provide a method body.

  • Definition format of the default method in the interface:

    • Format:

      public default return value type method name (parameter list ){}

    • Example:

      public default void show(){}

  • Interface Notes on the default method in:

    • The default method is not an abstract method, so it is not forced to be overridden. But it can be rewritten. When rewriting, remove the default keyword

    • public can be omitted, but default cannot be omitted:

      default void show(){}

1.3 Static methods in interfaces (JDK8)

  • Definition format of static methods in interfaces:

    • Format:

      public static return value type method name (parameter list){}

    • Example:

      public static void show(){ }

  • Notes on static methods in interfaces:

    • Static methods can only pass

      The interface name is called , which cannot be called by the 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)

A new private method with a method body is added in Java 9. This is actually The foreshadowing was laid 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 by private. This is the necessity of adding private methods in Java 9.

  • Definition format of private methods in interface:

    • Format 1 (non-static) : private return value type method name (parameter list) {}

    • Example 1:

      private void show() {}

    • Format 2 (static) private static Return value type method name (parameter list){}

    • Example 2:

      private static void method() {}

  • Notes on private methods in interfaces

    • The default method can call private static methods 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("高级工程师");
        }
    }

The above is the detailed content of How to implement Java interface composition update. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete