Type Security Test

Source: Internet
Author: User
Object o1 = new Object ();
Object o2 = new B ();
Object o3 = new D ();

Object o4 = o3;
B b1 = new B ();
B b2 = new D ();

D d1 = new D ();
// B b3 = new Object (); CTE
// D d2 = new Object (); CTE

B b4 = d1;
// D d3 = b2; CTE

D d4 = (D) d1;
D d5 = (D) b2;

// D d6 = (D) b1; RTE
// B b5 = (B) o1; RTE
B b6 = (D) b2;

Here, CTE indicates an error during compilation and RTE indicates an error during running.

Remember two points to cover all the above conversions:
1. An object can be converted to any of its base types without any additional syntax. This is a safe implicit conversion.
2. It is mandatory to convert an object to its derived type because an exception may occur during running -- RTE

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.