Reference to see a summary of the interface:
The interface is a sign .
For example, if you're hungry, you see a store with a KFC in front of you, and then you think you can go in and buy a burger.
KFC is the interface, we see this interface, we know that the store will sell hamburgers (Implementation interface).
So why do we have to define an interface, this shop can directly sell hamburgers AH (direct write implementation method), yes, this shop can sell fried chicken leg directly, but no hang KFC's signboard, we do not know what is inside the sale.
we might want to go in and ask, you don't sell hamburgers here (that's reflection). Obviously, the question of such a family is very troublesome (the reflection performance is very poor).
we may want to remember, XX road xxx sell hamburger, XX ... (hard coding), it's obvious that we have a lot of things to keep in mind (the code is exploding), and if there's a new store selling burgers, it's impossible to know (not to expand).
The interface is defined as follows:
[Modifier]interface< Interface Name >[extends< interface List >]{ data member; The default is public static final member function; Default is public abstract}
Note: Interfaces can implement multiple inheritance
The implementation of the interface is as follows:
[Access modifier]class class name implements[interface list]{public member function;}
Examples are as follows:
1 Interfaceinterface0{2 voidprint ();3 }4 classInterFace1Implementsinterface0{5 Public voidprint () {6System.out.println ("Interface INTERFACE0 implementation");7 }8 }9 Public classInterFace {Ten Public Static voidMain (String args[]) { OneInterFace1 inter =NewInterFace1 (); A inter.print (); - } -}
Multiple Inheritance of interfaces:
1. You can have an interface to inherit multiple interfaces, and then use a class to implement the interface
1 Interfaceinterface0{2 voidprint0 ();3 }4 Interfaceinterface1{5 voidprint1 ();6 }7 //interface InterFace2 Inheritance Interface0,interface18 InterfaceInterFace2extendsinterface0,interface1{9 }Ten //achieve class implementation interface One classAchieveImplementsinterface2{ A Public voidprint0 () { -System.out.println ("InterFace0 print0 Method"); - } the Public voidprint1 () { -System.out.println ("InterFace1 Print1 method"); - } - } + Public classInterFace { - Public Static voidMain (String args[]) { +achieve Inter =Newachieve (); A inter.print0 (); at inter.print1 (); - } - } - //Output: - //InterFace0 Print0 Method - //InterFace1 Print1 Method
2. You can also have a class to inherit multiple interfaces
1 Interfaceinterface0{2 voidprint0 ();3 }4 Interfaceinterface1{5 voidprint1 ();6 }7 //class achieve inherit Interface0,interface1 two interfaces8 classAchieveImplementsinterface0,interface1{9 Public voidprint0 () {TenSystem.out.println ("InterFace0 print0 Method"); One } A Public voidprint1 () { -System.out.println ("InterFace1 Print1 method"); - } the } - Public classInterFace { - Public Static voidMain (String args[]) { -achieve Inter =Newachieve (); + inter.print0 (); - inter.print1 (); + } A } at //Output: - //InterFace0 Print0 Method - //InterFace1 Print1 Method
Problems with Java Learning: interface implementations