Reference conversions between a base class and a subclass

Source: Internet
Author: User

A

As can be seen from the source code, subclasses can be directly assigned to the base class ("base class variable = Subclass Object"), the reference type of the base class to be passed to the subclass must pass the cast ("sub-class variable = (subclass name) base class object");

No conversions can be made between subclasses.

Two

The source code can be summed up:

When a base class has a variable of the same name as a subclass, the reference variable is the reference, and the variable that refers to which class is called;
When a base class has a method with the same name as a subclass, whichever is the type of the object, and the method of which class is called;
Conversion between a base class and a subclass: A base class variable can directly refer to a subclass object, or directly assign a subclass object to a base class variable;
However, when a base class object is to be converted to a subclass variable, it must be cast.
The conversion between a base class and a subclass is simply a conversion of a reference type, and it does not change the real object.

Reference conversions between a base class and a subclass

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.