Java-basic interface syntax (Set 1, 28th)

Source: Internet
Author: User



The following is a java4android video tutorial from marschen.


The methods used by interfaces actually reflect the essence of objects.
Interfaces are definitely the most important.
Main content.
1. What is an interface.
2. Basic interface syntax.

Computer ==>> USB interface. can be connected to MP3, mobile phone, refrigerator.
USB



Interface syntax. (It is a pure abstract class .)
1. Use interface definition.
2. All methods in the interface are abstract methods.
3. All methods in the interface have the public permission.

//************************************** *********************************//
====>>>> Defining interfaces is the standard for defining call objects. It is very important. <<<<<==
//************************************** **********************************//

1. Implement the interface using the implements keyword.
2. A class can implement multiple interfaces. (Unlike a class that can only inherit one parent class)
3. One interface can inherit multiple interfaces.
Implementation is a special inheritance

Program example

USB Interface

interface USB{public void read();public void write();}

Implement USB Interface

class USBPhoneimplements USB{public void read(){System.out.println("USBPhone read");}public void write(){System.out.println("USBPhone write");}}

Test 1

class Test{public static void main(String args[]){USBPhone usbPhone = new USBPhone();USB usb = usbPhone;usb.read();usb.write(); }}

Define WiFi Interface

interface WiFi{public void open();public void close();}

Use phone to implement USB and WiFi Interfaces

class Phoneimplements USB,WiFi{public void read(){System.out.println("Phone read");}public void write(){System.out.println("Phone write");}public void open(){System.out.println("WiFi  open ");}public void close(){System.out.println("WiFi Close");}}

Test 2

Class test {public static void main (string ARGs []) {// USB phone = new USB phone (); // USB = USB phone; // USB. read (); // USB. write (); phone = new phone (); USB = phone; // The interface can also be upgraded. USB. Read (); USB. Write (); WiFi = phone; wifi. open (); wifi. Close ();}}

3. One interface can inherit multiple interfaces.

The Code is as follows:

Interface

interface A{ public void funA();}

Interface B

interface B{ public void funB();}

Interface C inherited interface A and interface B

interface C extends A,B{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.