Java4android (basic syntax for interfaces)

Source: Internet
Author: User

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

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.