Java4android (basic interface syntax)

Source: Internet
Author: User

What is an interface? An interface is defined to define the standard of the called object.

Basic interface Syntax:

1. Use interface definition;

2. All methods in the interface are abstract methods;

3. All methods in the interface have the public permission.


Basic interface syntax (2 ):

1. Implement the interface using the implements keyword

2. One class can implement multiple interfaces

3. One 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 ();}
Class Phone implements USB, Wifi {// implements implement USB Phone implement all abstract methods in USB interface // implement interface public void read () {System. out. print ("read");} public void write () {System. out. print ("write");} public void open () {System. out. print ("Wifi Open");} public void close () {System. out. print ("Wifi close ");}}
Class Test {public static void main (String args []) {Phone phone = new Phone (); USB usb = phone; // converts the usb to the upper-right corner. read (); usb. write (); Wifi wifi = phone; // converts to wifi. open (); wifi. close ();}}

One interface can inherit multiple interfaces:

interface A{    Public void funA();}
interface B{    Public void funB();}
Interface C extends A, B {// C does not implement interfaces A and B, but inherits interfaces A and B. Therefore, C contains all the methods of interfaces A, B, and C. // when there is A class to implement the C interface, all methods A, B, and C must be rewritten as Public 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.