Java study note 12 --- parent class Object Reference pointing to Child class object, learning Note 12 ---

Source: Internet
Author: User

Java study note 12 --- parent class Object Reference pointing to Child class object, learning Note 12 ---

What happens when the object reference of the parent class does not point to the object of the parent class, but to the object of the subclass?

Assume that the parent class is Person and the subclass is Student. The following two rows are defined:

Student sTest = new Student ();

Person pTest = sTest;

PTest is the object reference of the parent class, And sTest is the object reference of the subclass. Both pTest and sTest point to a subclass object. So,

(1). If the subclass'sMember variablesIf the type and name of the member variables of the parent class are the same, when sTest is used for access, the member variables of the subclass are accessed. When pTest is used for access, access the member variables of the parent class;

(2). If the subclass'sStatic member variableIf the type and name of the static member variable of the parent class are the same, sTest accesses the static member variable of the subclass. When pTest is used for access, access static member variables of the parent class;

(3). If the subclass'sStatic Method RewritingThe static method of the parent class is called when sTest is called, the static member method of the subclass is called; When pTest is called, the static method of the parent class is called;(1), (2), and (3) are called hidden;

(4). If the subclass'sMember method RewritingThe member method of the parent class. When sTest is called, the member method of the subclass is called;When you call pTest, it also calls the member method of the subclass. In this case, it is called overwrite;

(5). If hidden variables or methods are used in the method when the unoverwritten parent class member method is called using sTest, the rules are the same as above;

 

The following is a simple example.

The Person class is the parent class, the Student class is the subclass, And the TestMain class is the test class. The Code is as follows:

The Person class code is:

Package human; public class Person {String name; int age; String gender; public String education; private String holobby; protected String residence; static String citizenship = "Chinese"; public Person () {} public void setName (String n) {this. name = n;} public String getName () {return name;} public void informationPrint () {System. out. println ("My name is (getName)" + getName (); System. out. prin Tln ("My name is (name)" + name); System. out. println ("I am" + getAge () + "years old"); if (getGender () = "female") System. out. println ("I am a girl"); else if (getGender () = "male") System. out. println ("I am a boy"); else System. out. println ("Something is wrong! "); System. out. println ("My holobby is" + holobby); if (citizenship = "Chinese") System. out. println ("I am a chinese"); // test: whether the static variable initializes else if (citizenship = "US") System before the constructor. out. println ("I am an American"); else System. out. println ("Oh, something is wrong");} // test: overwrite public void testModifierPublic () {System. out. println ("Person: Public");} // test: Hide public static void staMethodHide () {System. out. println ("Person: static Method ");}}

 

The code of the Student class is:

Package human; public class Student extends Person {String stuNumber; int score; // test: Hide String name = "ha"; static String citizenship = "US"; public Student () {} public Student (String n, String g) {super (n, g);} // test: Hide public void setName (String n) {this. name = n;} // test: Hide public String getName () {return name;} // test: overwrite public void testModifierPublic () {System. out. println ("Student: Public");} // test: super public void testSuper () {System. out. println ("Super:"); super. testModifierPublic ();} // test: Hide public static void staMethodHide () {System. out. println ("Student: static Method ");}}

  

The code of the TestMain class is:

Package human; public class TestMain {public static void main (String [] args) {Student sTest = new Student (); Person pTest = sTest; System. out. println ("The following is the result of using the subclass object sTest to access variables and call Methods:"); sTest. testModifierPublic (); System. out. println (sTest. name); System. out. println (sTest. getName (); System. out. println (sTest. citizenship); sTest. staMethodHide (); sTest. testSuper (); sTest. informationPrint (); System. out. println ("The following is the result of accessing variables and calling methods using the parent class Object pTest:"); pTest. testModifierPublic (); System. out. println (pTest. name); System. out. println (pTest. getName (); System. out. println (pTest. citizenship); pTest. staMethodHide ();}}

 

Output result:

1 The following is the result of using the subclass object sTest to access variables and call Methods: 2 Student: Public 3 ha 4 Student: getName () 5 ha 6 US 7 Student: static Method 8 Super: 9 Person: Public10 Student: getName () 11 My name is (getName) ha12 My name is (name) null13 I am 0 years old14 Something is wrong! 15 My Hober is null16 I am a chinese17 The following is the result of accessing variables and calling methods using the parent class Object pTest: 18 Student: Public19 null20 Student: getName () 21 ha22 Chinese23 Person: static Method
View Code

 

Analyze the following results:

(1 ).The first two statements are:

Student sTest = new Student ();
Person pTest = sTest;

The first statement defines that the subclass object references sTest and points to a subclass object. The second statement defines that the parent class Object references pTest and is assigned the value of sTest; the general memory structure is shown in Figure 1:

Figure 1

 

The name of the String member variable of the subclass is the same as that of the parent class. The name value of the subclass is "ha", and the name of the parent class is initialized to NULL by default; the String type static member variable citizenship has the same name as the citizenship of the parent class. The value of the sub-class citizenship is "US", and the value of the parent class citizenship is "Chinese ".

(2 ).The 4th statement is: sTest. testModifierPublic ();

The output result is 2nd rows: Student: Public

The statement in Row 3 is pTest. testModifierPublic ();

The output result is 18th rows: Student: Public

STest and pTest both call the testModifierPublic () method. The subclass overrides this method of the parent class. When sTest is called, it is clear that the method after the subclass rewriting is called;During the pTest call, because this method has been overwritten by the quilt class method, the method called after the subclass rewriting is also called.

(3 ).The 5th and 6 statements are:

System. out. println (sTest. name );

System. out. println (sTest. getName ());

The output result is 3rd, 4, and 5 rows:

Ha

Student: getName ()

Ha

13th and 14 statements are:

System. out. println (pTest. name );

System. out. println (pTest. getName ());

The output result is 19th, 20, and 21 rows:

Null
Student: getName ()
Ha

STest. name directly accesses the member variable name, sTest. getName () is the value of name indirectly obtained by calling the getName () method; both methods output "ha"; although name hides the name of the parent class, getName () the getName () of the parent class is overwritten, but the caller is sTest. Therefore, all variables and methods of sub-classes are used;

Although name is hidden, pTest is referenced by the parent class object. Therefore, the access is the name of the parent class, so the output is NULL. However, the getName () of the parent class is overwritten, therefore, the subclass method is called.

(4 ).7th and 8 statements are:

System. out. println (sTest. citizenship );

STest. staMethodHide ();

The output result is 6th and 7 rows:

US

Student: static Method

 

15th and 16 statements are:

System. out. println (pTest. citizenship );

PTest. staMethodHide ();

The output result is 22nd and 23 rows:

Chinese
Person: static Method

For static member variables and static methods, when hidden, the variables or methods of the subclass are accessed or called when referenced by the subclass object; when accessed or called by a parent class object, the variables or methods of the parent class are accessed or called.

(5 ).The 9th statements are: sTest. testSuper ();

Output result:

Super:

Person: Public

STest calls the subclass member method testSuper () and uses super. testModifierPublic ();,Super is used to explicitly call the method of the parent class, so the output is Person: Public.

(6 ).The 10th statements are: sTest. informationPrint ();

The output result is 10th to 16 rows:

Student: getName ()

My name is (getName) ha

My name is (name) null

I am 0 years old

Something is wrong!

My holobby is null

I am a chinese

<1> The subclass does not override the member method informationPrint () of the parent class;

<2>. Output rows 10th and 11. The name value obtained through the getName () method is "ha", and the output row 12th is NULL by directly accessing the name;

It indicates that the getName () subclass is called, but the name of the parent class accessed;

That is to say, when a subclass calls a member method that has not been overwritten by the parent class, if the member method body calls a method that has been overwritten by the quilt class, the subclass actually calls the method that has been overwritten by the subclass;

If a hidden member variable is accessed, the member variable of the parent class is actually accessed.

<3> the output line 16th shows that the accessed static member variables are also the variables of the parent class.

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.