Java Interface Classic Example

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.