Java Interface (Interface) Primary knowledge

Source: Internet
Author: User

Features of the interface:

1. Interfaces cannot be instantiated

2. The implementation class must implement all the methods of the interface

3. Implementation classes can implement multiple interfaces

4. Variables in the interface are static variables

5. Often use multiple inheritance in Java as a type

Example code:

Package cn.xzh.Interface;

Public interface USB {
Variable constants are represented by large writes
public static final String NAME = "interface";

public abstract void Service ();
}

-----------------------------------------------------------------------------

Package cn.xzh.Interface;

public class Usbfan implements usb{

@Override
public void Service () {
TODO auto-generated Method Stub
System.out.println ("Fan is running");
}

}

-----------------------------------------------------------------

Package cn.xzh.Interface;
/**

* @author Administrator
* Subclasses implement interfaces that must be implemented for all methods in the interface
*/
public class Usbdisk implements usb{

@Override
public void Service () {
TODO auto-generated Method Stub
System.out.println ("in charge");
}

}

---------------------------------------------------------------------

Package cn.xzh.Interface;

public class Test {

public static void Main (string[] args) {
TODO auto-generated Method Stub
USB US = new Usbdisk ();
USB US2 = new Usbfan ();
Us.service ();
Us2.service ();
}

}

------------------------------------------------

Test Run Results:

Charging in progress
The fan is running.

Java Interface (Interface) First knowledge

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.