Features of the interface:
1. Interfaces cannot be instantiated
2. The implementation class must implement all the methods of the interface
3. Implementation classes can implement multiple interfaces
4. Variables in the interface are static variables
5. Often use multiple inheritance in Java as a type
Example code:
Package cn.xzh.Interface;
Public interface USB {
Variable constants are represented by large writes
public static final String NAME = "interface";
public abstract void Service ();
}
-----------------------------------------------------------------------------
Package cn.xzh.Interface;
public class Usbfan implements usb{
@Override
public void Service () {
TODO auto-generated Method Stub
System.out.println ("Fan is running");
}
}
-----------------------------------------------------------------
Package cn.xzh.Interface;
/**
* @author Administrator
* Subclasses implement interfaces that must be implemented for all methods in the interface
*/
public class Usbdisk implements usb{
@Override
public void Service () {
TODO auto-generated Method Stub
System.out.println ("in charge");
}
}
---------------------------------------------------------------------
Package cn.xzh.Interface;
public class Test {
public static void Main (string[] args) {
TODO auto-generated Method Stub
USB US = new Usbdisk ();
USB US2 = new Usbfan ();
Us.service ();
Us2.service ();
}
}
------------------------------------------------
Test Run Results:
Charging in progress
The fan is running.
Java Interface (Interface) First knowledge