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