Assignment and coercion of type conversions between object references and interface references in the Java Development Manual learning process

Source: Internet
Author: User

Between object references:

    • A subclass reference can be assigned to a parent class reference.
    • The parent class reference needs to be cast before it can be assigned to a subclass reference.
    • For a cast of an object reference, a compilation can be done as long as the converted reference type is a derived or derived relationship from the converted target type. If you do not have these relationships to cast, compile an error .
    • Even if a compilation is passed, an error occurs if the object type that the referenced reference points to is inconsistent or incompatible with the converted target type (that is, the object type that the referenced reference points to cannot be converted to a different type other than itself or the parent class).

Between interface references:

    • A sub-interface reference can be assigned to a parent interface reference.
    • A parent interface reference requires that a type conversion be forced before it can be assigned to a subinterface reference.
    • Interface references that do not have an inheritance relationship can also be assigned after coercion of type conversions. This is different from the object reference.
    • Any two interface references can be coerced to type conversions without worrying about the compilation not passing (a little bit).
    • Even if the compiler passes, an error occurs if the converted interface refers to an object type that does not implement the converted interface.

The assignment between a class reference and an interface reference:

    • A class reference is assigned to an interface reference and does not require a forced type conversion, but the compile time checks whether the class implements the interface or, if not, compiles an error .
    • The interface reference is assigned to the class reference to enforce the type conversion, but if the type conversion is to be coerced, the target type of the converted class must implement the interface, otherwise, compile an error . Even if the compilation is passed, an error occurs if the converted interface reference does not match or is incompatible with the object type that the converted target type is pointing to.

Assignment and coercion of type conversions between object references and interface references in the Java Development Manual learning process

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.