Transformational operations do not change the form of objects in memory

Source: Internet
Author: User

1 /*2 Requirement: Design a simple Java program that proves that the cast statement does not change its form in memory3 idea: Design a parent-child class, create a subclass object, transform the subclass up to see if the object has changed in memory before and after the transformation4 */5 6 classexchangetest7 {8      Public Static voidMain (string[] args)9     {TenChild C =NewChild (23); One System.out.println (c); AFather f =(Father) C; - c.show (); - f.show (); theSystem.out.println (f);//both C and F point to the same object in memory, and the object is an instance object of child -     } - } -  + classFather - { +     intWorknum; AFather (inti) { atWorknum =i; -     } -     voidShow () { -System.out.println ("Father" +worknum); -     } - } in  - classChildextendsFather to { +     intStudynum; -Child (inti) { the         Super(i); *Studynum =i; $     }Panax Notoginseng     voidShow () { -SYSTEM.OUT.PRINTLN ("Child" +studynum); the     } + } A  the  + /* - Summary: $ when a subclass is transformed upward, the subclass object is still a subclass object in memory, even if a parent class variable points to the object, which is still a subclass object.  $  -  - bugs that occur at run time the ----------------------------------------------------------------------------------- - exchangetest.java:31: Error: Unable to apply constructor father in class father to the given type;Wuyi Child (int i) { the                     ^ - Required: int Wu found: no parameters - cause: The actual parameter list and formal parameter list length are different About 1 Errors $  - Error Reason: Subclass constructor Child (int i) {} The first line has a hidden default statement super (); The statement invokes the - the null parameter constructor for the parent class because the parent class wrote father (int i) {} so the original hidden father () {} - No, so super () in the subclass constructor cannot find father () {} so error A ----------------------------------------------------------------------------------- + */
Results:

  

Summary:   when the subclass is transformed upward, the subclass object is still a subclass object in memory, even if a parent class variable points to the object, the object is still a subclass object.

Transformational operations do not change the form of objects in memory

Related Article

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.