Java inheritance and Polymorphism

Source: Internet
Author: User

Java inheritance and Polymorphism

The principle of direct calling of java methods may be incorrect.

When a method of an object is called directly, the object will obtain the class information. The object will be found in the class information object. This method cannot be found. The object for obtaining the parent class information will continue searching. is distance achieved through reflection or other?

If the above principles are not explained

Create a subclass object and assign a value to a parent class pointer variable. Use the parent class pointer variable to drop the run method. The run method is private in the parent class or subclass. In this case, the run method of the parent class is called instead of the subclass. run method


Reflection jar package

Package extend; import org. junit. test; import cn. nailsoul. reflect. util. reflectUtil; public class extendTest {@ Testpublic void testAcccessMembers () {System. out. println ("=========== testAcccessMembers ============ "); // directly modifying attributes in the parent class always changes the attributes of the parent class Persion p = new Student (); p. setName ("Perion. name: "+" Zhang San "+" Student. name: "+ (Student) p ). name); System. out. println (p. getName (); // when modifying an attribute, the attributes defined in the class declared on the left are not related to the attributes defined in the class to which the modified object belongs. age = 25; System. out. println ("Perion. age: "+ p. age + "Student. age: "+ (Student) p ). age ); // if the method is private in the subclass or parent class when a method is called, it is not the method that overwrites the method in the class declared on the left. // only the method in the subclass and parent class is used. at the same time, if the method is not private, the method that overwrites the method that has an execution subclass of the subclass does not execute the method of the parent class. // calling a method in the parent class also follows the above special p. privateSay (); p. publicSay (); p. run () ;}@ Testpublic void testReflectAccessMembers () throws Exception {// The reflection call method returns the same result as the direct call method. // if the method is private in the subclass or parent class when a method is reflected, the method of the reflected class is called. // only when this method is not private in both the subclass and parent classes, the System calls the method System of the reflected object. out. println ("=========== testReflectAccessMembers ============"); Student stu = new Student (); ReflectUtil. callMethod ("privateSay", null, null, Persion. class, stu, false); ReflectUtil. callMethod ("publicSay", null, null, Persion. class, stu, false); ReflectUtil. callMethod ("privateSay", null, null, Student. class, stu, false); ReflectUtil. callMethod ("publicSay", null, null, Student. class, stu, false ); // when no reflection method is found in the reflected class, an exception is thrown. The class information object to be reflected does not contain the method information of the parent class to be reflected. // only saving Class A information does not report an error. Class A's parent class information. All method codes are saved in the method area. Through the Class A information object, you can find all methods of Class A in the method area. code // ReflectUtil. callMethod ("setName", new Class [] {String. class}, "James", Student. class, stu, false); // when attributes are modified by reflection, the attributes defined in the class to be reflected are modified. ReflectUtil is irrelevant to the attributes defined in the class to which the reflected object belongs. setFieldValue ("age", 30, Persion. class, stu, false); System. out. print ("Reflect. age: "+ ReflectUtil. getFieldValue ("age", Persion. class, stu, false); System. out. println ("Persion. age: "+ (Persion) stu ). age + "Student. age "+ stu. age); stu = new Student (); ReflectUtil. setFieldValue ("age", 30, Student. class, stu, false); System. out. print ("Reflect. age: "+ ReflectUtil. getFieldValue ("age", Student. class, stu, false); System. out. println ("Persion. age: "+ (Persion) stu ). age + "Student. age "+ stu. age); // when the reflected class cannot find the class information object to be reflected when an exception is thrown and the class information object to be reflected does not have the attribute information of the parent class to be reflected. // ReflectUtil. setFieldValue ("money", 30, Student. class, stu, false);} public class Persion {protected String name; private int age; public long money; public String getName () {System. out. println ("Persion. getName () "); return name;} public void setAge (int age) {System. out. println ("Persion. setAge () "); this. age = age;} public void setName (String name) {System. out. println ("persion. setName () "); this. name = name;} public int getAge () {System. out. println ("persion. getAge () "); return age;} private void privateSay () {System. out. println ("Persion. privateSay () ");} void publicSay () {System. out. println ("Persion. publicSay () ");} void run () {System. out. println ("Persion. run () "); privateSay (); publicSay () ;}} public class Student extends Persion {protected String name; private int age; public void setAge (int age) {System. out. println ("Student. setAge () "); this. age = age;} public int getAge () {System. out. println ("Student. getAge () "); return age;} private void privateSay () {System. out. println ("student. privateSay () ");} void publicSay () {System. out. println ("student. publicSay ()");}}}


Running result

=========== TestReflectAccessMembers ================
Persion. privateSay ()
Student. publicSay ()
Student. privateSay ()
Student. publicSay ()
Reflect. age: 30 Persion. age: 30 Student. age0
Reflect. age: 30 Persion. age: 0 Student. age30
=========== TestAcccessMembers ================
Persion. setName ()
Persion. getName ()
Perion. name: Zhang San Student. name: null
Perion. age: 25 Student. age: 0
Persion. privateSay ()
Student. publicSay ()
Persion. run ()
Persion. privateSay ()
Student. publicSay ()

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.