I am publishing here and I Dennisboys learning exchange letters, we are mainly talking about multi-state issues in Java. I would like to thank my good friends to give me a chance to explain the problem of my ability. I hope that some of the mistakes in the explanation or some need to discuss hope to inform me.
The last article about polymorphism, please refer to the << Java polymorphism instance of the previous written "Explanation) >>.
*********************************************************** ******************************************************************************************************* *********************************************************** ************** From the beginning to the end, put your code n times, painted N maps (on the function address diagram of the parent class and subclass), there are some confused TEST T = new test (); base b = new base (); b = t;
/ * Question 1: Here is the reference to the child class, which is not to say that the method of calling the parent class is equal to the method of calling subclasses? Is it a Display2 () method that is called subclass if b.display2 () is not a Display2 () method of the subclass? And the subclass has a display2 () method, why can't you call? (I tried to write the code, and I really said that it is wrong.)
For the above questions, you have a text seem to be interpreted. But I am not very clear. I don't know if it is the following paragraph.
Quote: At the same time, some people may ask, is the function address in the subclass is not in the virtual function table? I am very glad that you ask such questions, but the virtual function table seen by the parent class is not that function item, because he is absolutely invisible for the parent class reference.
I now understand that if the method is made or adding a method in subclavab, is it invisible to the parent class? Is it understood? ? * /
Question 2: (Test) b) .display (); another blur is what you said to force the syntax syntax? ? I don't understand what is ((TEST) b)?
But come back (how do I think this example is to inherit ??), this example makes me a deep understanding of the inheritance, I feel that the polymorphism is still a "half solution, (Don't say that I am stupid) may also I didn't know how to use the polymorphism, but please reply to my two questions, I hope you will reply to the above two questions (combined with me to study your code) make me a better understanding of polymorphism . . *********************************************************** ************************************************************************************************************** *********************************************************** *************
*********************************************************** **************************** The interpretation of zostapo begins ************************ *********************************************************** ************
You should really thank me, because I have worked hard, because the reason is not able to save successfully, harm my rewriting once, no way, who told you is my good friend.
First of all, thank you for your patience.
Below I will make a simple description of the article I last written and your question but a more complicated instructions.
The article I wrote last time has a writing error. I have been revised online, tell you here:
There is such a few words in the last original text ********************************************* * // The following call will be wrong File: t.display2 (); file: // should be like this ((TEST) b) .display2 (); ************* ****************************
These sentences have errors on writing: should be changed to the following:
******************************************* // Call below [No] will be wrong (here more [no] words) file: t //t.display2 (); ################################################################################################################################################################################################################################# ########### In fact, I have no relationship with this two lines above, but for you very well understood dynamic, you can compare the above code with the following . ##################################: // below this line call Will be wrong file: //b.display2 (); here added a line: // should be like the following ((TEST) B) .display2 (); ************** ******************************
The topic we have officially begun, is explained on your question. Here I explain as much as possible to understand, in fact, it is too complicated to the specific implementation of OOP, this problem has to be involved in compilation The problem, the main one is the object memory layout problem.
Since the second problem is relatively simple, I reverse the order of answering questions.
********* Question Two ********* You are right, I solve it here, but I solve it here is not only inherited. It can be that the inheritance you said is just a way to explain the dynamic problem. Because inheritance and polymorphism is the object-oriented concept, not writing a little article, you can explain it.
Simple and inaccurate, dynamicity is intended to be indivisible, and if there is no inheritance, it will not talk about polymorphism. So I said that you are right, but you don't really understand the role of my example (55555555 ~~~~~~~~~~~~~ My heart blood)
********* Question One ************* The first thing to explain is that your understanding of this problem is incorrect.
This problem is more complicated, I try to illustrate this problem with some non-standard vocabulary. Because it is easy to understand. I don't solve too much theory here, because I am now studying the JVM specification, because many things Java and C are different, although both achieved very similar. Above this, I am based on some of my personal practices, including theoretical aspects and procedures. But I don't dare to take the C 's set to tell you, I actually tell you that you don't know how to understand. I mainly tell some basic knowledge, you can remember it, I will explain it when you study deeper, maybe you will understand it. Inly, after inheritance, the subclass will reset its own virtual function table, and the items in this virtual function table are composed of two parts. Virtual functions and subclats inherited from the parent class their own virtual functions.
Remember a simple and complicated rule, a type reference can only refer to the method and variables of the reference type itself. You may say that this rule is not right, because the parent class references the child class object, the reference is the method of the subclass. I tell you that this rule is still established for this situation. Relax your brain, don't think about something chaotic, listen to me carefully.
Yes, when you come here, I assume that you have the above rules in addition to the virtual function call, and all other static function references, and variable references are understood. Let's start our weight level description. Virtual function reference. The following is a description of the object memory layout in the JVM specification. I didn't translate I think you can understand. Anyway, I have no problem in English now. If I don't understand the dictionary. . The Java Virtual Machine does not require any particular internalstructure for objects In Sun's current implementation of the Java Virtual Machine, a reference to a class instance is a pointerto a handle that is itself a pair of pointers: one to a table containing the methods of THE OBJECT THATER TO The class Object That Repesents The Type of the Object, And The Other To The Memory Allocated from The Java Heap for the Object Data.
According to this, I know that JVM about polymorphism support solution is almost the same as C , only Many compilers in C are putting type information and virtual function information in a virtual function table, but use some kind Technology is distinguished.
So when you use the parent class reference to the subclass, the JVM has used the type information adjustment conversion generated by the compiler. Here you can understand this, which is equivalent to setting the function contained in the parent class into an invisible in the virtual function table. Note that some function addresses in the virtual function table have been overwritten in the subclass, so the virtual function item address in the object virtual function table has been set to the address of the method body completed in the subclass.
The above is why the parent class references points to subclass objects, and some methods can be called, and some methods cannot be called.
The virtual function call is indirectly called by a virtual function table, so it is possible to achieve polymorphism.