Article From:https://www.cnblogs.com/dreamcoding/p/9906596.html

【Learning Notes]Tang Dashi-Java Programming Lecture 4.6 Interfaces for Classes, Packages and Interfaces

Interface

  • Interfaces, conventions for certain characteristics
     Define interface
      All methods are automatically public abstract
     Implementing interface implements
      Multiple inheritance can be achieved
      It has nothing to do with the inheritance relationship of classes
  • Interface-oriented programming,Not implementation-oriented
     Flyable f = new Bird()
     JavaThere are a lot of interfaces.
    Flyable is an interface Bird class to implement this interface.

The role of interfaces

  • 1.The same behavior of unrelated classes can be achieved through interfaces without considering the hierarchical relationship between these classes, thus realizing multiple inheritance in a sense.
  • Interfaces can specify the methods that multiple classes need to implement
  • Through the interface, we can understand the object’s interaction interface without knowing the corresponding class of the object.

Example

  • Here we give the definition of an interface:
interface Collection{
    void add(Object obj);
    void delete(Object obj);
    Object find(Object obj);
    int size();
}
  • Usually the interface ends with able or iable, and the table name interface can accomplish certain behavior.
  • The interface declaration can also include access rights to the interface and a list of its parent interfaces. The complete interface declaration is as follows
[public]interface interfaceName [extends listOfSuperInterface]{
    ......;
}
  • The public specifies that any class can use this interface, and by default only classes defined in the same package can access this interface.
  • extendsThe clause is basically the same as the extends clause in class declaration, except that an interface can have multiple parent interfaces separated by commas, while a class can only have one parent. Subinterfaces inherit all of the parent interfacesconstantAnd method

Interface type

  • Interfaces can be used as a reference type. Any instance of a class that implements the interface can be stored in variables of the structure type through which methods in the interface implemented by the class can be accessed. The Java runtime system dynamically determines which class of methods to use
  • As a data type, you don’t need to know the specific class corresponding to the object. For example, the interface Collection defined above and the class FIFOQueue that implements the interface, in the following example, we use Collection as a reference type.
class InterfaceType{
    public static void main(String args[]){
        Collection c = new FIFOQueue();
        ......
        c.add(obj);
        ...
    }
}

Constants in interfaces

  • Interface weight can contain constant definitions
  • The format of constant definition is: type NAME = value;
  • Where type can be any type, NAME is a constant name, usually capitalized, and value is a constant value
  • Constants defined in an interface can be shared by multiple classes that implement the interface, which is the same as consts defined in C with # define and C++ with const.
  • Defined in the interfaceConstants have attributes of public, static, final

enumeration

  • Since JDK 1.5, enumerations can be used
enum Light{ Red, Yellow, Green }
  • Use:
    Light light = Light.Red;
    switch( light ){ case Red:...break; }
  • JavaEnumeration is implemented in class and can be used in complex ways.

Java8Interface in

  • Java8Above all, the interface members may also be:
     staticMethod
     Method with implementation body (default method)
      The advantage of the default method is that it provides a default implementation, and subclasses can be rewritten in implements without rewriting.

Leave a Reply

Your email address will not be published. Required fields are marked *