General rules for object type conversion, object conversion rules

Source: Internet
Author: User

General rules for object type conversion, object conversion rules

1. the type of a subclass object can be converted to its parent class, that is, a subclass object can be referenced as a parent class object. This conversion is safe and can be automatically implemented by the Java compiler.

2. the type of a parent class object cannot be converted down to its subclass type, that is, a parent class object cannot be used as a subclass object. However, when the parent class Object references a subclass object, it can be forced type conversion. Otherwise, the compilation can pass, but the exception of ClassCastException will be thrown during runtime.

3. assignment or type conversion cannot be performed between irrelevant classes. That is, the type conversion of a class is limited to two classes with inheritance relationships.

4. Interface polymorphism. The interface cannot be instantiated, but you can declare a reference to the interface. An interface reference can be replaced by an instance object of a specific class that implements an interface. That is, an interface reference can be assigned a value to an object of the class that implements the interface, or an interface reference can be used as a method parameter, use the class object that implements the interface as the real parameter.

 

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.