Detailed Java Interface

interface

Java language reference type, is a collection of methods, if the inner class encapsulates the member variables, constructors and members of the method, the internal interface is mainly packaging methods , it contains abstract methods (JDK 7 and older), The default method and static methods (JDK 8), private method (JDK 9).

Interface definition:

It defined the class in a similar manner, but using interfacekeywords. It will also be compiled into .class files, but it must be clear that it is not a class, but another reference data types.

Reference data types: arrays, classes, interfaces.

Use interface:

It can not create objects, but can be implemented ( implementssimilar to 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.

Definition Format:

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

final: keywords, showing the meaning of the final state, if the modification variable that becomes constant

Constant: Use public static final

Interface have default modifier constants: public static Final may be omitted

 

Implementation of the interface

Relationship between classes and interfaces to achieve relationship, i.e. class implements an interface , such interface implementation class may be referred to, may also be referred subclass interface. Implemented acts similar inheritance, the format is similar, but different keywords, implemented using implementskeywords.

Non-abstract subclass that implements the interface:

  1. All the abstract methods in the interface must be rewritten.

  2. It inherits the default interface methods that can be called directly, can also be rewritten.

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

 

Abstract methods defined in an interface

Abstract: Method no method body, and modified using the abstract keyword

The default modifier: public abstract may be omitted

public  abstract return type method name (parameter);

The default method defined in the interface - at a method thereof Method

 

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

 

 

public  default return type method name (parameter) { 
    Method member 
}

 

The definition of static methods of the interface

 

public  static return type method name (parameter) { 
   Method member 
}

 

Private method defined in the interface

 

Private non-static method

private return type method name (parameter) { 
   Method member 
}

Private static method

Private static return type method name (parameter) { 
   Method member 
}

 

 

 

 

Features Interface

Interface is a kind of reference data types

Interface can only define constants and methods (abstract methods, static methods, the default method, private method)

Interfaces can not define member variables and constructor

Interface not create an object can only be used by its implementation class

 

Guess you like

Origin www.cnblogs.com/libinhong/p/10990347.html