Section II What is an interface

Source: Internet
Author: User

What is interface teacher: Wang Shaohua QQ Group No.: 483773664 Learning Goal
    1. Mastering the definition and use of interfaces

    2. Understanding the difference between an interface and an abstract class

First, what is the interface

In real life, we often hear the word interface, such as the USB interface, what does it mean? We take the USB interface for example, regardless of the manufacturer of the USB memory, we can use the USB interface to let the computer recognize it, this is why? This is because these manufacturers follow a set of identical standards and follow this standard to generate USB devices.

In software, the interface is also a specification and standard, they can constrain the behavior of the class, is a collection of some method features, but there is no method of implementation. Therefore, the interface can also be regarded as a special "abstract class".

Second, the definition of the interface

Unlike the definition class, the keyword class is not used, and the keyword interface is used.

(a) define the syntax

The syntax is as follows

1234 [修饰符] interface接口名 extends父接口1,父接口2...{    零个到多个常量定义...    零个到多个抽象方法定义...}

Description

    1. Modifiers can only be public or omitted

    2. As long as the interface name conforms to the Java Identifier Naming specification, it is recommended that multiple meaningful words be concatenating, capitalized, and that there is no connector between words and words.

    3. An interface can have multiple direct parent interfaces, but cannot inherit classes.

    4. The attributes defined in the interface can only be constants and, regardless of whether the public static final decoration is used, the properties in the interface will always be decorated with these three modifiers

    5. No constructors and initial blocks in the interface

    6. method in an interface, only an abstract method

(ii) Define an interface
123456 publicinterface InterfaceDemo {    //接口里定义的属性只能是常量    int MAX_LENGTH = 50;    //接口里定义的方法只能是抽象方法    voidgetLength();}
(iii) Verification:whether the public static final modifier is used, the properties in the interface will always be decorated with these three modifiers

The properties in the interface are by default using the public static final decoration, so even if another class is under a different package, the interface's constant properties can be accessed through the interface

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/7E/FF/wKioL1cPbICR9sJsAAAnaJqY1jk990.png " data_ue_src= "E:\My knowledge\temp\f34d0ef7-92d8-40df-9184-6435bd667dc8.png" >

third, the inheritance of the interface

The inheritance of interfaces is not the same as class inheritance. The inheritance of an interface is multi-inheritance, separated by multiple interfaces.

Similar to class inheritance, a subinterface extends the parent interface and obtains the parent interface's abstract methods, constant properties, and so on

12345678910111213141516171819 public interface InterfaceA {    String PRO_A="InterfaceA";    public void testA();}public interface InterfaceB {    String PRO_B="InterfaceB";    public void testB();}public interface InterfaceC extends InterfaceA,InterfaceB{    String PRO_C="InterfaceC";    public void testC();}public class TestInterfaceExtends {    public static void main(String[] args) {        System.out.println(InterfaceA.PRO_A);        System.out.println(InterfaceB.PRO_B);        System.out.println(InterfaceC.PRO_C);    }}
Iv. use of interfaces(i) Purpose of the interface

Interface cannot create an instance

An interface can be used to declare a reference-type variable, and the variable must point to an object that implements the implementation class for this interface;

The main purpose of the interface is implemented by the implementation class

(ii), the implementation of the interface

interface implementation, using the keyword: implements

A class can implement one or more interfaces, and multiple interfaces are separated by semicolons

Java has a single inheritance, multi-implementation features

123 [修饰符] class类名 extends 父类 implements接口1,接口2...{   类体部分}

Once a class implements one or more interfaces, the class must fully implement all the abstract methods of those interfaces, otherwise the class must be defined as an abstract class

1. All implementations
123456789101112131415161718192021 public class DemoD implements InterfaceA,InterfaceB,InterfaceC{    @Override    public void testC() {        // TODO Auto-generated method stub            }    @Override    public void testB() {        // TODO Auto-generated method stub            }    @Override    public void testA() {        // TODO Auto-generated method stub            } <>

Section II What is an 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.