Java14-java Syntax Basics (13) interface

Source: Internet
Author: User

Java14-java Syntax Basics (13) interface

First, the interface

1, the role of the interface

Java is not supported for multiple inheritance and only supports single inheritance for security reasons and for simplifying the structure of the program. However, in many cases, it is not possible to describe complex problems by simply relying on single inheritance. To make the class hierarchy of Java programs more reasonable and more consistent with the nature of the actual problem, the Java language provides interfaces to implement multiple inheritance .

2, the definition and implementation of the interface

1) Definition of the interface

Interface is one of the most important concepts in Java, a special class defined with the interface keyword, also known as an interface type. It is used to describe all services provided by the system, but does not include a specific implementation.

[Modifier] Interface Interface Name [extends parent interface List]{

Static constant data member declaration; With Static and final retouching

abstract method declaration; To modify with an abstract

}

Emphasize:

(1) No construction method for interface

(2) The method declared in the interface is abstract by default

(3) The members declared in the interface are static and final by default

2) Implementation of the interface

[modifier] class name Implement Interface list {

member variables;

Member methods;

}

Emphasize:

(1) A class can have multiple interfaces

(2) If a class implements an interface, all the abstract methods in that class must be implemented

3, interface and interface, interface and the relationship between the class

(1) interface can inherit more than one interface

Interface A extends b,c{}

(2) class can implement multiple interfaces

Class A implements b,c{}

(3) class-inheriting class implements interface

Class A extends B implements C {}

4. Multiple Inheritance Implementation cases

Camera and Internet access, but different phones use different technologies. So defining the functional interface is used to develop uniform standards, and the specific implementation by the handset manufacturers to complete their class structure. The class structure model diagram that implements multiple inheritance is as follows:

5. Summary

1) What issues should be noticed in implementing the interface?

(1) If an abstract class implements an interface, it can not implement an abstract method in the interface;

(2) If an ordinary class implements an interface, it implements all the abstract methods in the interface;

(3) When referencing an interface, a method in a class that implements an interface is redundant, do not create a reference with an interface, because a reference created with an interface cannot refer to its own independent method of implementing the class itself.

2. How does Java implement multiple inheritance?

inherits the parent class through subclasses , and implements multiple interfaces to implement multiple inheritance.

Java14-java Syntax Basics (13) 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.