Implementation and interface considerations Interface-

package cn.learn.Interface;

public interface MyInterfaceA {
   public abstract void methodA();
   public abstract void methodAbs();
   public default  void methodDefault(){
       System.out.println("aaa");
   }
}
package cn.learn.Interface;

public interface MyInterfaceB {
   public abstract void methodB();
   public abstract void methodAbs();
   public default  void methodDefault(){
      System.out.println("bbb");
   }
}
. 1  Package cn.learn.Interface;
 2  // multiple inheritance interface
 3  // wherein methodAbs is no conflict, since the method is not an abstract method body 
. 4  
. 5  public  class ExtendsInterfaceImpl the implements ExtendsInteface {
 . 6      // override default method, java12 not with the default keyword 
. 7      @Override
 . 8      public  void methodDefault () {
 . 9  
10      }
 . 11  
12 is      @Override
 13 is      public  void Method () {
 14  
15      }
 16  
. 17      @Override
 18 is     public  void methodA ({)
 . 19  
20 is      }
 21 is  
22 is      @Override
 23 is      public  void methodB () {
 24  
25      }
 26 is      // abstract method override conflicts 
27      @Override
 28      public  void methodAbs () {
 29  
30      }
 31 is }
. 1  Package cn.learn.Interface;
 2  / * 
. 3    class distinction and the interface
 4    between the class and class 1 is single inheritance, only one direct parent
 5    2 between the interface and the multi-class implementation, a class can implement multiple interfaces
 6    between 3 interface and the interface is a multiple inheritance, an interface you want a variety of other abstract methods interface
 7  
8  below excuse, containing four methods
 9  1.methodA
 10  2.methodB
 11  3.method
 12  ** 4.methodAbs source simultaneously with the pretext of a and B
 13  
14  *
 15  Note: Multiple inheritance class interface, the interface multi father inherited the conflict does not matter, but the default method is repeated there will be problems, you must override
 16  
. 17   * / 
18 is  
. 19  public  interface ExtendsInteface the extends MyInterfaceB, MyInterfaceA {
20     public abstract void method();
21 
22     @Override
23     default void methodDefault() {
24         
25     }
26 }
. 1  Package cn.learn.Interface;
 2  / * 
. 3      interface Note:
 4  1 interface is not a static block static {} and construction methods
 . 5  
. 6  2. While the direct parent class is a unique, but at the same time a class multiple interfaces
 7  formats:
 . 8  public MyInterfaceImpl the implements interfaces a class name, the name of the interface B {
 9          // all abstract methods cover
 10  }
 . 11  
12 is  a plurality of interface implementation class 3. If implemented in duplicate abstract methods, then only overwritten
 13     once (if there is a plurality of interfaces default method must be overridden in such cover)
 14  
15  4. implement all abstract methods not realized if all of the interfaces
 16    implement the abstract class must be class
 . 17  
18 is  6. If a subclass method among the direct parent of inheritance, the method and the default interface conflict, priority class method with the parent
 19    format:
 20   public class name of the subclass extends parent class implements the interface {
 21      Method body
 22    }
 23    NOTE: default interface methods need to rewrite this case, since a lower priority than the parent
 24  
25  
26 is   * / 
27  // Java all directly or indirectly class is a subclass of Object, following 
28  public  class MyinterfaceImpl the extends Object the implements MyInterfaceA, MyInterfaceB {
 29      // same abstract method override two interfaces 
30      @Override
 31 is      public  void methodAbs () {
 32          the System.out .println ( "rewrite the same two interfaces abstract methods" );
 33      }
 34 is  
35     @Override
 36      public  void methodDefault () {
 37 [  
38 is      }
 39  
40      // overwrite the port A port B all abstract methods 
41 is      @Override
 42 is      public  void methodA () {
 43 is          System.out.println ( "overwrite a method a " );
 44 is      }
 45  
46 is      @Override
 47      public  void methodB () {
 48          System.out.println (" method B overwritten " );
 49      }
 50 }
/ * 
Interface content summary 
1. In fact, there is a member variable public static final modification, is the global constants 
  and constants must be assigned, and can not change the 
  format to be fully capitalized, separate words with an underscore, 

2. interface is the most important abstract methods, format: 
  public abstract method return type name (parameter list); 
  Note: all implementation class must override all interface methods, except abstract class 

3. start java 8, the interface allows to define the default method, format: 
  public defalut return value type the method name (parameter list) { 
    method body   
  } 
  Note: the default method may be overwritten 

4. from the start java 8, the interface allows to define a static method, format: 
  public static method return type name (parameter list) { 
    method body   
  } 
  Note : static should be called by the name of the interface, can not be achieved by a static method call to a modified class 

5. Starting java 9, the interface allows the definition of proprietary methods, formats 
  ordinary private methods: 
  public private method name return value type (parameter list) { 
    method body 
  } 
  static private method: 
  } 
  public static return type private Method name (parameter list) {
    Method body 
  Note: Method private, only you can call the interface, implementation class or can not be used by others 




* /

 

Guess you like

Origin www.cnblogs.com/huxiaobai/p/11462162.html