Java notes (4) interface

Source: Internet
Author: User

Interfaceusb{voidstart (); voidstop ();}classc{ Public Static voidWork (USB u) {u.start (); System.out.println ("In Work");    U.stop (); }}classUsbdiskImplementsusb{@Override Public voidstart () {System.out.println ("U disk starts to work"); } @Override Public voidStop () {System.out.println ("USB Stick Stops working"); }    }classPrinterImplementsusb{@Override Public voidstart () {System.out.println ("Printer started working"); } @Override Public voidStop () {System.out.println ("Printer stopped working"); }    } Public classAbsDemo01 { Public Static voidMain (string[] args) { c.work ( new Usbdisk ()); C.work (New Printer ()); }    }

Analysis:

1. In the interface, there are two abstract methods, void Start () and void Stop (). In fact, in these two abstract methods, the preceding public abstract is omitted.

2. In Class C, a work class method is created. In the main () method, C.word (New Usbdisk ()) and C.word (New Printer ()) are used. In this case, we use the class name. Method name to invoke the method, using the Static keyword knowledge. A class that uses static adornments is called a class method, which can be called directly by the class name or by an instance object. (It is recommended to call directly with the class name, since the steps instantiated with new can be omitted directly, more concise and more convenient)

3. In the main () method,c.work (new Usbdisk) uses the knowledge of the parent class declaration and subclass instantiation when transferring parameters. We can understand it this way:USB u = new Usbdisk, the declaration part is an interface, the assignment part is the object u that implements the class, and the method of invoking the implementation class when invoking the overridden abstract method .

Java notes (4) interface

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.