Java8 series (four) static methods and the default method (reprint)

Static methods and the default method

We can Comparator source interface, we see a number of methods such statement similar to the following

    // default keyword modified the default method 
    default Comparator <T> thenComparingInt (ToIntFunction <? Super T> keyExtractor) {
         return thenComparing (comparingInt (keyExtractor));
    }
    //Comparator接口中的静态方法
    public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
        return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE;
    }

Which thenComparingInt () method is a default, it uses the default keyword modification. This is a new feature introduced Java8: interface methods and static methods can declare a default.

The default method raises the issue of multiple inheritance

Prior to this, Java classes in only supports multiple inheritance, do not support multiple inheritance. Now, with the default method, you can be another way to achieve multiple inheritance behavior class, namely a class implements multiple interfaces, and these interfaces have declared their default method.

There raises a problem of multiple inheritance, imagine if a class inherits from the default method they are declared multiple interfaces, but these default method used is the same function signature, then the program is running, type which will choose a method call it?

Listing One:

    @Test
    public void test2() {
        new C().hello();//result: hello from D
    }

    interface A {
        default void hello() {
            System.out.println("heelo from A");
        }
    }

    interface B extends A {
        default void hello() {
            System.out.println("heelo from B");
        }
    }

    class D implements A{
        public void hello() {
            System.out.println("hello from D");
        }
    }

    class C extends D implements A, B{
    }

Listing output is a hello from D, can be seen, the parent class D class C, the parent connections A, B parent interface defines a same signature function   Hello ()  , and finally the actual call the parent category D method declaration.

Listing Two:

    @Test
    public void test4() {
        new I().hello();//result: heelo from G
    }

    class I implements G, H { }
    
    interface G extends E {
        default void hello() {
            System.out.println("heelo from G");
        }
    }
    
    interface H extends E { }

    interface E {
        default void hello() {
            System.out.println("heelo from E");
        }
    }

The output is two Listing hello from G, it can be seen parent interface class I G, E parent interface defines a same signature function  Hello ()  , the last actual call connection G is a method for the parent declared.

Listing Three:

    @Test
    public void test3() {
        new F().hello(); //result: heelo from E
    }

    interface A {
        default void hello() {
            System.out.println("heelo from A");
        }
    }

    interface E {
        default void hello() {
            System.out.println("heelo from E");
        }
    }

    class F. the implements A, E {
         public  void Hello () {
             // Here interfaces A and E have no inheritance, one has to call interface A or Method E explicitly otherwise not compile 
            E. Super .hello ( );
        }
    }

Listing in three classes hello F must explicitly override the parent interface () method, or by detecting the compiler can not, because the compiler can not determine the default method parent interface interfaces A and E in which the parent priority.

In this case, the default method if you want to call a parent interface, you can use   the interface name. Super . The default method name  in this way calls.

to sum up

Java8 new features: the interface can declare a default and static methods.

Further, the interface default method brings multiple inheritance problem that if a class signature functions using the same methods inherited from a plurality of places (such as another class or interface), can be judged by three rules:

  • Methods in the class with the highest priority. Method priority class or parent class declared above is the default method of any claims priority.
  • If you can not be judged based on the first, then the higher priority sub-interface: function signature is the same, there is a default method is most preferred specific implementation of the interface, that is, if B inherits A, then B is even more specific than A.
  • Finally, if you still can not judge, inherits multiple interfaces class must explicitly call the desired coverage and methods, which explicitly choose to implement a default method of use (call syntax:   interface name super default method name.  ).

Author: Zhang Xiaofan
Source: https://www.cnblogs.com/qingshanli/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the apparent position of the original article page connected otherwise, the right to pursue legal responsibilities. If you feel that there is help, you can tap the bottom right corner [Recommended].

Guess you like

Origin www.cnblogs.com/jwcz/p/11783001.html