java--interface

Source: Internet
Author: User

First, interface overview

    An interface is a collection of functions, which can also be seen as a data type, which is a more abstract class than an abstract class.

The interface only describes the methods that should be available, and there is no specific implementation, specifically implemented by the interface implementation class (equivalent to the subclass of the interface) to complete. This separates the definition and implementation of the function and optimizes the program design.

format: Unlike the class that defines classes , the interface keyword is required for the interface definition.

The definition of the interface is still a. java file, although a. class file is still generated after the compilation of the interface keyword that is used when declaring.

This allows us to consider an interface as a special class that contains only functional declarations.

Public Interface Interface Name {

Abstract methods

}

(1) interface methods are all abstract methods , the direct new interface to invoke the method is meaningless , Java is not allowed to do so

(2) The relationship between class and interface is to implement the relationship, that is, the class implements the interface. The implemented action is similar to inheritance, except that the keywords are different and implemented using implements

(3) Other types ( implementation Class ) when the interface is implemented, it is equivalent to declaring:"I should have the functionality in this interface." the implementation class still needs to override the method to implement the specific functionality.

Second, the characteristics of the interface members

    A: between classes and Classes : inheritance , a class can inherit directly from only one parent class , but supports multiple inheritance

B: between classes and Interfaces : Only implementation relationships , a class can implement multiple interfaces

C: between interface and interface : only inheritance , an interface can inherit multiple interfaces

Three, the advantages of the interface

    1. The relationship between class and interface, implementation of the relationship, and is a multi-implementation, a class can implement multiple interfaces, class and class is an inheritance relationship, Java inheritance is a single inheritance, a class can have only one parent class, breaking the limitations of inheritance.

2. External provision of rules

3. Reduce the coupling of the program (can achieve modular development, define the rules, everyone to implement their own modules, improve the efficiency of development)

Iv. differences between interfaces and abstract classes

    1. Commonality:

continuous extraction, extraction of abstract, no concrete implementation of methods , can not be instantiated (cannot create objects)

2. Difference 1: Relationship with the class

        classes and interfaces are implementation relationships, and are multiple implementations, a class can implement multiple interfaces, and classes are inheritance relationships with abstract classes. Inheritance in Java is a single inheritance, multi-layer inheritance, a class can inherit only one parent class, but can have Grandpa class

3. Difference 2: Member

A. Member variables

Abstract classes can have member variables, or they can have constants

interface can have constants only, default modifier public static final

B. Member methods

Abstract classes can have abstract methods, or they can have non-abstract methods

interface can have abstract methods only, default modifier public abstract

C. Construction method

Abstract classes have construction methods that provide a subclass of

Interface has no construction method

java--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.