Java object-oriented (six) Interface

First, the interface

  •   Interface : is the Java language reference type, is a collection of methods, internal package if the class member variables, constructors and members of the method, the internal interface is mainly packaging methods , contains an abstract method ( the JDK 7 and older ), the default method and static methods ( the JDK 8 ), private method ( the JDK 9 ).
  •   Definition of the interface: it defined the class in a similar manner, but using the interface keyword. It will also be compiled into .class files, but it must be clear that it is not a class, but another reference data types.  
  •   Use interface: it can not create an object, but can be implemented ( the implements , like inherited). A class that implements the interface (can be seen as a subclass of the interface), all of the abstract methods in the interface need to implement to create the class object, you can call the method, or else it must be an abstract class.

 Tips : reference data types: arrays, classes, interfaces and the like.

Second, the definition format

  Syntax:

public interface Interface name { 
    // abstract methods 
    // default method 
    // static method 
    // private method 
}

  1, comprising an abstract method (JDK7 +)

    Abstract method : Use the abstract keyword modifications, can be omitted, there is no method body. The method used for sub-class implementation.

   Demo:

1 public interface InterFaceName {
2   public abstract void method();
3 }

 

  2, the method comprising the default and static methods (JDK8 +)

    Default method : modified using the default, not be omitted for the subclass or the subclass call override.

   Static methods : using static modification, calls for direct interface.

   Demo:

. 1  public  interface InterfaceName {
 2      public  default  void Method () {
 . 3          // execute statements 
. 4      } 
 . 5      public  static  void method2 () {
 . 6          // execute statements 
7      }
 8 }

 

  3, the method comprising private and private static methods (JDK9 +)

    Private methods : the use of private modification, the default method for the interface or static method call.

    Demo:

. 1  public  interface InterfaceName {
 2      Private  void Method () {
 . 3          // execute statement 
4      }
 5 }

 

  4、

Third, the basic realization

  1, the realization of outlined

    Relationship between classes and interfaces to achieve relationship, i.e. class implements an interface , such interface implementation class may also be called, and may also be referred to as a sub-class interface.

   Implemented acts like inheritance, the format is similar, but different keywords, implemented using implements keyword.

   Non-abstract subclass that implements the interface :

    •  All the abstract methods in the interface must be rewritten.
    •    It inherits the default interface methods that can be called directly, can also be rewritten.

   Realization format :

class name of the class implements the interface name { 
    // interface abstract methods must override [] 
    // default override interface method [optional] 
}

  

  Using 2, abstract methods

      Abstract methods in the interface must be fully realized.

  Use 3, the default method

      It can be inherited, can be rewritten, a second election, but can only be called by the object implementation class.

  Use 4, static methods

      Static associated with .class file, use only the interface name calling , can not be achieved by the class name of the class or object that implements the class is called.

  5, using private methods

      •   Private methods : Only the default method can be called;
      •    私有静态方法:默认方法和静态方法可以调用;

     如果一个接口中有多个默认方法,并且方法中有重复的内容,那么可以抽取出来,封装到私有方法中,供默认方法去调用。

     

四、接口的多实现

五、接口的多继承

    一个接口能继承另一个或者多个接口,这和类之间的继承比较相似。接口的继承使用 extends 关键字,子接口继承父接口的方法。

   如果父接口中的默认方法有重名的,那么子接口需要重写一次。

六、其他成员特点

  •   接口中,无法定义成员变量,但是可以定义常量,其值不可以改变,默认使用 public static final 修饰。
  •       接口中,没有构造方法,不能创建对象。
  •       接口中,没有静态代码块。

七、

Guess you like

Origin www.cnblogs.com/niujifei/p/11369679.html