Interface in java (interface)

Interface (based on jdk1.9)

definition:

A common standard specification

Properties and methods that can be included in the interface

Constant: public static final data type constant name = data value;

note:

  • Constants must be assigned when they are defined, and once assigned, they cannot be changed
  • Constant names are fully capitalized, and multiple words are separated by underscores

Method:
abstract method: public abstract return value type method name (parameter list);

note:

  • The implementation class must cover all abstract methods in the interface, unless the implementation class is an abstract class

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

note:

  • The default method can be inherited or overridden by the implementation class

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

note:

  • It can only be called by the interface name, neither the implementation class name nor the implementation class object can be called

Private methods: divided into general private methods && static private methods

General private method: private return value type method name (parameter list) {                                      method body                           } } Static private method: private static return value type method name (parameter list) {                                      method body                           }





note:

  • Private methods can only be accessed inside the interface, and cannot be accessed by implementation classes and other classes

note:

  • The jdk1.7 interface can only contain constants and abstract methods
  • jdk1.8 adds default methods and static methods on the basis of 1.7
  • jdk1.9 adds private general methods and private static methods on the basis of 1.8
Interface considerations
  • [Constructor], [Static Code Block], [Construction Code Block] are not allowed in the interface, but local code blocks are allowed
  • If there are [repeated abstract methods] among the multiple interfaces implemented by the implementation class, the implementation class only needs to be overridden once
  • If there is a [duplicate default method] among multiple interfaces implemented by the implementation class, the implementation class must cover the repeated method
  • If there are multiple interfaces implemented by the implementation class [duplicate static methods] The implementation class does not need to perform any operations, because the static methods in the interface can only be called by the interface name and have nothing to do with the implementation
  • When the direct parent class of the implementing class has the same name as the default method in the implemented interface, the method in the parent class will be accessed first
The relationship between class and interface
  • There is an inheritance relationship between classes

Note: Only [single inheritance] is supported between java class and class, and the class can only have one direct parent class

  • The relationship between class and interface is realization,

Note: A class can implement multiple interfaces

  • There is an inheritance relationship between interface and interface

Note: The interface and the interface support multiple inheritance
          . When the default method in multiple parent interfaces is repeated, the sub-interface must override the method [and with the default keyword]


               If there are any shortcomings in the article, please leave a comment below and update it after you see it. Thank you!

Guess you like

Origin blog.csdn.net/weixin_45864391/article/details/108245914