What is an interface? An interface is defined to define the standard of the called object.
Basic interface Syntax:
1. Use interface definition;
2. All methods in the interface are abstract methods;
3. All methods in the interface have the public permission.
Basic interface syntax (2 ):
1. Implement the interface using the implements keyword
2. One class can implement multiple interfaces
3. One interface can inherit multiple interfaces
Example:
Interface USB {// defines the interface public void read (); public void write ();}
Interface Wifi {// defines the Wifi interface public void open (); public void close ();}
Class Phone implements USB, Wifi {// implements implement USB Phone implement all abstract methods in USB interface // implement interface public void read () {System. out. print ("read");} public void write () {System. out. print ("write");} public void open () {System. out. print ("Wifi Open");} public void close () {System. out. print ("Wifi close ");}}
Class Test {public static void main (String args []) {Phone phone = new Phone (); USB usb = phone; // converts the usb to the upper-right corner. read (); usb. write (); Wifi wifi = phone; // converts to wifi. open (); wifi. close ();}}
One interface can inherit multiple interfaces:
interface A{ Public void funA();}
interface B{ Public void funB();}
Interface C extends A, B {// C does not implement interfaces A and B, but inherits interfaces A and B. Therefore, C contains all the methods of interfaces A, B, and C. // when there is A class to implement the C interface, all methods A, B, and C must be rewritten as Public void funC ();}