What is an interface? Defines an interface, which defines the criteria for calling an object
Basic syntax for the interface:
1. Use interface definition;
2. The methods in the interface are abstract methods;
3. The methods in the interface are public permissions
Basic syntax for Interfaces (ii):
1. Implement the interface using the Implements keyword
2. A class can implement multiple interfaces
3. A single 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 ();}
classPhoneImplementsusb,wifi{//implements implementation of Usbphone USB interface//implement all abstract methods in an interface Public voidRead () {System.out.print ("Read"); } Public voidwrite () {System.out.print ("Write"); } Public voidOpen () {System.out.print ("Wifi Open"); } Public voidClose () {System.out.print ("Wifi Close"); }}
class test{ publicstaticvoid main (String args[]) { new Phone (); = Phone; // Upward Transformation usb.read (); Usb.write (); = Phone; // Upward Transformation Wifi.open (); Wifi.close (); }}
An interface can inherit multiple interface examples:
Interface a{ void Funa ();}
Interface b{ void Funb ();}
Interface extends a,b{//C does not implement A/b interface, but inherits A, B. So c inside there are all methods of A, B, C interface,// when there is a class to implement the C interface, it is necessary to replicate A, B, c all methods void FunC ();}