Java polymorphism and method coverage analysis

Source: Internet
Author: User
There should be no stranger to Polymorphism. It is an old friend who develops object-oriented systems. But old friends may also be "upset", huh, huh. Sometimes you don't pay attention to it. For example, the following example (thank Hayden ). You can choose not to look at the following answer, but run it in your mind to see if your thoughts are consistent with the actual results. Public class Polymorphism {
Public static void main (String [] args ){
A B = new B ();
B. fb ();
}
} Class {
Public (){

}
Public void fa (){
System. out. println ("class a: Function fa Runing ......");
}

Public void fb (){
System. out. println ("class a: Function fb Runing ......");
Fa ();
System. out. println ("class a: Function fb Stop ......");
}
} Class B extends {
Public B (){
}
Public void fa (){
System. out. println ("class B: Function fa Runing ......");
}

Public void fb (){
System. out. println ("class B: Function fb Runing ......");
Super. fb ();
System. out. println ("class B: Function fb Stop ......");
}
}

The following figure shows the running result: class B: Function fb Runing ......
Class a: Function fb Runing ......
Class B: Function fa Runing ......
Class a: Function fb Stop ......
Class B: Function fb Stop ......

How are you doing? If the results are exactly the same as you think, congratulations, you have a preliminary understanding of polymorphism, at least at the syntax level. However, do not be "proud". Can you parse the results? Let's take a look at the program process:
1. Run the main Function, create the B object, call the method fb of B, and print out "class B: Function fb Runing...", which is reasonable.
2. Execute super. fb () and call the method fb of the parent class a. First, print "class a: Function fb Runing...". Expected
3. Execute the fa () method and print out "class B: Function fa Runing...", er? It's strange, why not execute the fa () method of A, but the fa () in subclass B?The Method of Class A is currently executed, so the virtual machine should find the Method Table of Class A, and find the Method of Class A fa?Hard to solve ~
4. Print "class a: Function fb Stop..." and return
5. Print"Class a: Function fb Stop... ", return, the program ends.
Now the problem is clear, that is, the Method Table that the virtual machine looks for when executing the Class A Method is actually A subclass B. Why? In fact, as long as we understand the java method calling method, this problem will be solved. In a Java virtual machine, each time a new thread is started, the virtual machine allocates a java stack for it, and every time a thread calls a Java method, the VM will press a new frame in the java stack of the thread to store parameters, local variables, and other data. The method being executed is called the current method of the thread, and its stack frame is the current frame.
Well, when we call a method, what parameters do we need to input to the current frame? Simple: isn't the parameter list of the method clearly stated? Well, for C language, this statement is correct, but it is not true for object-oriented languages such as C ++, Java, and Python. Do you still remember the "this" pointer ?! Good,In Java, the current object is pushed into the current frame when all Instance methods are called, the Java virtual machine uses this parameter to determine the currently used class (by determining the object type).
In the preceding example, when calling B. fb () in main, the current object to be pushed in is naturally a Class B object, which is recorded as B. When super. fb () is called in fb () of B, the pressed object is the just-pressed object, that is, B. Similarly, when fa () is called in fb of A, B is also pressed. Therefore, when calling fa () using the invokevirtual command, find the method table of B (the type of the current object B is B), and then execute the fa of Class B.
This phenomenon is particularly common in constructor, because constructor implicitly uses the constructor that calls the parent class, if the constructor of the parent class calls the instance method (such as the fa of a), and the Sub-class overwrites the instance method (such as the fa of B ), the result is often not what we want. Therefore, we 'd better not use the polymorphism method in the constructor. Otherwise, Debug will be very painful :)

Transfer http://hi.baidu.com/daping_zhang/blog/item/2bba09fa10bc489759ee90bc.html

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.