Java upward Transformation

Source: Internet
Author: User
Tags export class

General understanding and upward Transformation:

That is, sub-classes are transformed into parent classes.

    1. ClassA
    2. {
    3. }
    4. ClassBExtendsA
    5. {
    6. }
    7. A B =NewB ();

This is an upward transformation.

 

Upward transformation can be as simple as the following statement:

Shape S = new circle ();

Here, create a circle object and assign the reference to s cycle immediately. This seems to be an error (assign a type to another type); but there is actually no problem, because by inheritance, circle is a shape. Therefore, the compiler does not generate error messages when it recognizes this statement.

Suppose you call a base class method (it has been overwritten in the export class );

S. Draw ();

You may think that drawing () of shape is called again; because it is a shape reference after all, how does the compiler know how to do other things? Due to the later binding (polymorphism), the circle. Draw () method is correctly called.

 

Summary

1. assign a subclass object reference value to a superclass object to reference a variable to call a dynamic method.

Derivedc C2 = new derivedc ();
Baseclass a1 = c2; // baseclass base class. derivedc is a subclass inherited from baseclass.
A1.play (); // play () is defined in baseclass and derivedc. This method is overwritten by the subclass.

Analysis:
* Why can an object instance of the subclass type be overwritten with a superclass reference?
Automatic upward transformation. With this statement, the compiler automatically moves the subclass instance up to become a general type baseclass;
* A. Play () will execute subclass or parent class definition methods?
Subclass. During runtime, the corresponding method is obtained based on the actual type referenced by the object. So there is polymorphism. Objects of a base class are referenced by different subclass objects. Different actions are performed when this method is executed.

When a1 = c2, there are still two handles, A1 and C2, but A1 and C2 have the same data memory block and different function tables.

2. You cannot reference a parent class object to a subclass object to reference a variable.

Baseclass a2 = new baseclass ();
Derivedc C1 = a2; // Error

In Java, upward transformation is automatically performed, but downward transformation is not. We need to define it as mandatory.
C1 = (derivedc) A2; forced conversion, that is, downward transformation.

3. Remember a simple and complex rule. A type reference can only reference methods and variables contained in the reference type.
You may say that this rule is incorrect, because when the parent class references a subclass object, the method of the subclass is executed.
In fact, this is not a conflict, because later binding is adopted, and the subclass method is called according to the type during dynamic operation. If the subclass method is not defined in the parent class, an error occurs.
For example, in addition to inheriting the functions defined in baseclass, The derivedc class also adds several functions (such as myfun ())

Analysis:
When you use the parent class to reference and point to a subclass, the JVM has actually used the type information generated by the compiler to adjust and convert the type information.
As you can understand, it is equivalent to setting functions not contained in the parent class to invisible from the virtual function table. Note that some function addresses in the virtual function table may have been rewritten in the subclass, therefore, the virtual function Project address in the object virtual function table has been set as the address of the method body completed in the 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.