Java Development Manual The 9th chapter of the Learning process interface

Source: Internet
Author: User
Tags event listener

member variables in the interface:

Unlike classes, you can define only constant properties in an interface, and you do not have to use public, statict, and final adornments, because even if you do not use these modifiers, the compile time is added, that is, the member variable in the interface implies "public static final". In addition, the value must be given at the same time as the declaration.

Method in the interface:

The method in the interface must be abstract, because the interface simply represents a contract that represents what the function of the class implementing it is, and the implementation is done by the class that implements it.

The methods in the interface must be public, abstract, and cannot use final and static as member variables in the interface.

Use of interface references:

The interface cannot be instantiated through new, but it can be pointed to an object that implements the interface through a reference to the interface type.

Unlike type references, any two-interface reference can be coerced to type conversions, and the target interface type that does not need to be converted has an inheritance relationship with the interface type before the conversion. Because the class in which an object resides can implement multiple interfaces that are not related, these interfaces can point to their objects, but if the object that the interface points to does not meet the requirements, the runtime produces an exception.

The assignment between a class reference and an interface reference:

  • a class reference to an interface reference does not require a forced type conversion, the compile-time check whether the class directly or indirectly implemented the interface, if it is compiled through, you can run;
  • The interface reference is assigned to the class reference to be forced type cast, but not casually converted, and must be the target class to implement the interface.

Notice the No. 203 page about comparing the contents of the interface with the abstract class.

Callback for the interface:

The implementation of interfaces and callbacks is often used in the event listener processing model.

Event A occurs when the code that handles event A is executed. However, determining when an event A and when to execute the code is fixed, can be written first, and event A's specific implementation code is open to other developers to write. There can be many different implementations that can be registered with the specific implementation to be processed.

Use of instanceof:

Instanceof is used to check whether the object pointed to by the reference can be treated as a specified type, but it cannot be used to test which particular type the object is, because the type of the object and its parent test can pass.

If the test reference is a null value, the result always returns false.

In cases where instanceof are not allowed to be tested, the reference type that participates in the test must be able to be cast to the type or interface type of the test to be instanceof by compiling.

Java Development Manual The 9th chapter of the Learning process 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.