Interface is really bad to understand!!!!! Well, let me summarize:
The first thing to understand is that the interface is an independent existence, and the class is different, so the definition of the interface directly is:
Access control Interface Interface Name [extends interface list] {
the variable in the//interface is the default public static final decoration .
abstract methods;//The methods in the interface are all modified by the public abstract, note!!! Abstract and private final static coexist
internal classes;//interfaces can have internal classes
static method;//jdk8 Support
default method;//jdk8 support
}
1 Packagecom.xt.imp.test;2 3 Public InterfaceAnimals {4 5 Public Static Final intI=0;6 7 Public Abstract voideat ();8 9 classheart{Ten One } A - Public Static voidsleep () { - the } - - Public default voidWork () { - + } -}
Access rights control: only public and friendly interface names and public decorated interface names are consistent
[extends interface list] means that interfaces can inherit multiple interfaces, cannot inherit classes, and can only inherit classes from previous classes
1 Package com.xt.imp.test; 2 3 Public Interface extends Imammal,ib {/// interfaces can inherit multiple interfaces, cannot inherit classes, and can only inherit class 45}6 compared to previous classes 7 Interface The access control on the front of the ib{//interface can only be public-friendly, public decorated only in a single file 8 9 }
There is no construction method in the interface, and the abstract class has a constructor method, the abstract class cannot be instantiated, but it can inherit its construction method
No static code block in interface, non-static code block
A class can implement multiple interfaces.
To implement an abstract method in an interface, you must strictly follow the overriding rules
1 Packagecom.xt.imp.test;2 3 Public classWhalesImplementsimammal,animals {//class can implement multiple interfaces4 5 @Override6 Public voidMove () {7System.out.println ("Mobile ... ");8 }9 Public Static voidMain (string[] args) {Ten System.out.println (imammal.i); One } A - //To implement an abstract method in an interface, you must strictly follow the overriding rules - @Override the Public voideat () { - - } - +}
If a class implements an interface, but does not implement all the abstract methods in the interface, the class must be an abstract class.
If the same abstract method is defined in more than one interface, only one of them can be implemented in the implementation class
An interface callback describes a phenomenon where an interface-declared variable points to an object whose implementation class is instantiated, and the interface variable can invoke an abstract method in the interface.
Interface does not have a constructor method, cannot create its own object, but can reference an object that implements the class
1 Packagecom.xt.imp.test;2 3 Public classStudensImplementsImammal {4 5 Public Static voidMain (string[] args) {6Imammal im =NewStudens ();7 System.out.println (IM.I);8 Im.move ();9 }Ten One @Override A Public voidMove () { -System.out.println ("mobile". "); - } the}
Function-type interface:
1 Package com.xt.imp.test; 2 3 @FunctionalInterface 4 Public Interface functioninterface {56 void ran (); 7 }
A summary of interface knowledge points in Java