An interface in Java is a special abstract class whose member variables are public static final and the member method is public abstract. Interface is a bridge between module and module communication. The following is an example of a computer motherboard, video card, and graphics interface to illustrate the role of the interface. We all know that the motherboard provides a number of interfaces, you can plug some hardware into the interface, through the bus to connect with the CPU, so as to coordinate work. The manufacturer of these hardware devices is different, but the products manufactured by any manufacturer should conform to the standard of the interface on the motherboard. The graphics interface code is as follows: interface videocard{
void display ();
String GetName ();
The manufacturer's video card should implement the standard of the motherboard graphics interface, code as follows: Class Angda implements videocard{
String name;
Angda () {
Name= "Angda ' s Videocard";
}
Angda (String name) {
This.name=name;
}
public void display () {
System.out.println ("Angda ' s videocard is running");
}
Public String GetName () {
return name;
}
The code for the motherboard: class mainboard{
String CPU;
Videocard VC;
void Setcpu (String CPU) {
This. CPU=CPU;
System.out.println (CPU);
}
void Setvideocard (Videocard vc) {
THIS.VC=VC;
System.out.println (Vc.getname ());
}
void Run () {
System.out.println (cpu+ "is running");
Vc.display ();
System.out.println ("Mainboard is running");
}
Entry for program: Class computer{
public static void Main (string[] args) {
Angda a=new Angda ();
Mainboard m=new mainboard ();
M.SETCPU ("Intel ' s CPU");
M.setvideocard (a);
M.run ();
}
}