Transformation operations do not change the form of objects in the memory. transformation is not an object form.

Source: Internet
Author: User

Transformation operations do not change the form of objects in the memory. transformation is not an object form.

1/* 2 requirement: design a simple java program to prove that the forced conversion statement cannot change its form in the memory. 3 idea: design a parent-child class and create a subclass object, transform the Child class up and check whether the object changes in the memory before and after the transformation 4 */5 6 class ExchangeTest 7 {8 public static void main (String [] args) 9 {10 Child c = new Child (23); 11 System. out. println (c); 12 Father f = (Father) c; 13 c. show (); 14 f. show (); 15 System. out. println (f); // both c and f point to the same object in memory, and the object is a Child Instance Object 16} 17} 18 19 class Father20 {21 int worknum; 22 Father (int I) {23 worknum = I; 24} 25 void show () {26 System. out. println ("Father" + worknum); 27} 28} 29 30 class Child extends Father31 {32 int studynum; 33 Child (int I) {34 super (I ); 35 studynum = I; 36} 37 void show () {38 System. out. println ("Child" + studynum); 39} 40} 41 42 43/* 44 conclusion: 45 after the subclass is transformed upwards, the subclass object remains a subclass object in the memory, even if a parent class variable points to this object, this object is still a subclass object. 46 47 48 The bug49 running 50 ExchangeTest occurs. java: 31: Error: unable to apply the constructor Father in the class Father to the given type; 51 Child (int I) {52 ^ 53 Required: int54 found: No parameter 55 cause: the actual parameter list and format parameter list have 56 different lengths. 1 error 57 58 error cause: the first line of the subclass constructor Child (int I) {} has a hidden default statement super (); this statement will call the null parameter constructor of the 59 parent class, because the parent class has written Father (int I) {}so the hidden Father () {}60 is gone, therefore, the super () in the subclass constructor cannot find the Father () {}. Therefore, the 61 limit 62 */error is returned */
Result:

  

Summary: After the subclass is transformed up, The subclass object remains a subclass object in the memory. Even if a parent variable points to the object, the object remains a subclass object.

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.