Several questions about how to override the parent class method and overwrite the parent class variables in the subclass during inheritance

Source: Internet
Author: User


Suppose that the subclass reloads the method of the parent class and overwrites the member of the subclass.

Create a subclass object instance and convert it to a parent class.




Example 1

 
Public Class Parent {public void Init () {system. out. println ("1 init parent"); this. demo ();} public void demo () {system. out. println ("2 demo parent ");}}

 
Public class son extends parent {public void Init () {super. init (); system. out. println ("3 init son"); this. demo ();} public void demo () {system. out. println ("4 demo son");} public static void main (string [] ARGs) {// parent P = new son (); // 1son son = new son (); // 2son. init (); // Init (son )}}


When 1 is executed, the result is:

1 init parent
4 demo son
3 init son
4 demo son

When 2 is executed, the result is:

1 init parent
4 demo son
3 init son
4 demo son

Scenario 1 and scenario 2 are actually similar. Because new son () is used to create an instance, after the transformation, the init () in the parent () this in the method is also considered to point to the son instantiation object in the heap area, so this. demo () still accesses the method that is reloaded in the subclass.


Example 2

 
Public Class Parent {public string name = "Tom"; Public void Init () {system. Out. println (this. Name );}}

 
Public class son extends parent {public string name = "Jack"; Public void Init () {super. init (); system. out. println (this. name);} public static void main (string [] ARGs) {// 1 currently running class son = new son (); son. init (); // Init (son) system. out. println ("#" + son. name); // 2 the current running class parent P = new son (); p. init (); system. out. println ("**" + P. name );}}

When the code runs 1, I guess the result is:

Jack

Jack

# Jack

But the actual result is:

Tom
Jack
# Jack

In the parent class, this. Name points to the name value in the parent class.


We cannot see the actual situation in the memory, but we can see through debug that there are two name variables with the same name in the son object.



This result can be explained in only a few cases.

1. This does not point to the instance created by son in the heap.

2. Other mechanisms are used in the memory when an object is created to ensure the generation of such results.


I have read the book "deep understanding of JVM advanced features and best practices of Java Virtual Machine", which seems to have come up with some clues.

It can be seen that when accessing the member variable, the program stops searching after detecting the member variable of the parent class.

For methods, because the content is stored in the method area, each object should be issued through its own this to reference unique binding. So it is almost explained.

The book "deep understanding of JVM advanced features and best practices of Java Virtual Machine:

Http://www.jb51.net/books/163531.html#down


The above is just a little bit of personal speculation.

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.