interface, definition of interface

Source: Internet
Author: User

Interface interface, as the name implies, is for a thing to communicate with another thing to provide the channel (means), for example, our current operating system is the Windows system, we see the interface and the various functions of the button are graphical user interface, interface is the user and the interface of the application internal interaction. The interface actually has two meanings: First, the feature set of the method that a class has, is a kind of logical extraction, the second is the Java interface, the structure exists in the Java language, there is a specific syntax and structure, the former is called "interface" the latter is called "Java interface". In this section we are studying the Java interface.

The interface in Java is the declaration of a series of methods, and is a collection of features of some methods. An interface only method features do not have a method implementation, so these methods can be implemented in different classes, and these implementations can have different behaviors (functions). Interfaces have the following characteristics:

(1) Only single inheritance is supported in Java. If you want to implement multiple inheritance, you can implement it through an interface.

(2) encapsulation. An interface provides a way for a class to hide the details of a particular thing it handles, and to publish only the properties it must support. You only need to know the method and its arguments that you call, without knowing the internal implementation.

(3) interface-oriented, such as a, b implementation of interface C, in the parameters of a method can write C (can represent a also can represent B), to the time when dynamically passed a or B instance can be.

Interface Definition: The interface contains constants and extraction methods, no variables and other member methods, once the interface is defined, all classes can implement the interface, and because the interface supports multiple inheritance, more than one class can implement multiple interfaces at the same time. The specific definition format of the interface is as follows:

[Public] Interface Interface Name [extends multiple parent interfaces]

{

[Public] [Static] [final] Type constant name = value;

[Public] [Abstract] Returns the type method name ([parameter]);

}

Where the default properties in the interface are constants, and the constants must be public constants by default, not allowed to have variables and ordinary methods, so the constant modifier cannot write the concrete implementation, so the modifier before the method, public and abstract can also be omitted. For example, define a interface:

Interface a{

int p=4.14;

void Show ();

}

The attribute p for interface A is a public constant, and show () is a public extraction method. It is important to note that since P is a constant, p must be assigned an initial value at the time of definition.

Interface definition, it is possible to indicate its encapsulation. If it is public, the interface can be implemented anywhere, and if there is no public, it is the default state and can only be accessed by the class in the same package.

The interface supports multiple-inheritance interfaces separated by commas, and the father must also be an interface. For example:

public interface father{

void Fn1 ();

void Fn2 ();

}

public interface mother{

void Fn3 ();

void Fn4 ();

}

Public interface Son extends Father,mother

{

void Fn5 ();

}

In the Father interface there are fn1 () and fn2 () Two image extraction methods, The Mother interface has FN3 () and FN4 () two extraction methods. There is a fn5 () extraction method in the son interface defined, since his father is the father and mother interface, there should be 5 extraction methods in the son interface. If a class implements the son interface, it must implement the 5 methods in Son,father,mother.

PS: The father of the interface must be an interface and cannot be a class.

interface, definition of 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.