Java polymorphic/Overloaded methods--a discussion caused by a problematic code

Source: Internet
Author: User

Directly on the code, look at the code to find out how bad the base is. Refer to Http://www.cnblogs.com/lyp3314/archive/2013/01/26/2877205.html and http://hxraid.iteye.com/blog/428891 and the Deep Java Virtual machine

  This question is the ultimate way for Java to get dynamic bindings? The knowledge involved is the integration of inheritance, polymorphism, overloading, method invocation.

 Public classPoly { Public Static voidMain (string[] args) {A A1=NewA (); A A2=NewB (); b b=NewB (); C C=NewC (); D d=NewD ();        System.out.println (A1.show (b));        System.out.println (A1.show (c));        System.out.println (A1.show (d));        System.out.println (A2.show (b));        System.out.println (A2.show (c));        System.out.println (A2.show (d));        System.out.println (B.show (b));        System.out.println (B.show (c));    System.out.println (B.show (d)); }}classA { PublicString Show (D obj) {return("A and D"); }     PublicString Show (A obj) {return("A and a"); }}classBextendsA { PublicString Show (B obj) {return("B and B"); }     PublicString Show (A obj) {return("B and A"); }}classCextendsB {}classDextendsB {}

Directly to the conclusion:

A and AA and AA and DB and AB and AA and DB and BB and BA and D

Excerpt conclusion:

(1) All private methods, static methods, constructors, and initialization methods <clinit> all use static binding mechanisms. The symbolic reference of the calling method in the constant pool is indicated at the compiler stage, and only one constant pool resolution is required for the JVM to run.

(2) The invocation of the class object method must adopt a dynamic binding mechanism during the run.

First, the "appropriate" method is found based on the object's declaration type (the type of the object reference). The steps are as follows:

① This method is most appropriate if you can match a method in the declaration type to exactly the same method signature (the parameter type is the same).

② in the case that article ① can not be satisfied, to find a way to "do". The standard is to match the parameter types after they are automatically transformed. If multiple auto-transformed method signatures F (a) and F (b) are matched, the following criteria are used to determine the appropriate method: The parameters passed to the F (a) method can be passed to F (B), and F (a) is most appropriate. Conversely F (B) is most appropriate.

③ if the "appropriate" method is still not found in the claim type, the compilation phase fails.

Then, the corresponding method table is found based on the actual type of object created in the heap, and the lookup mode is the same as the claim type.

Interpretation:

  • System.out.println (A1.show (b)); The A1 is declared as a type A, there is no exact matching method in a, a is transformed to a, the A.show (a) method is called, the A1 actual type is type A, and the Run method is the A.show (a) method.
  • System.out.println (A1.show (c)); The A1 is declared as a type A, there is no exact matching method in a, the C-up is transformed to a, the A.show (a) method is called, the A1 is the actual type a type, and the method is A.show (a).
  • System.out.println (A1.show (d)); The A1 is declared as a type A, there is an exact match in a method, the A.show (d) method is called, A1 the actual type is type a, and the method is the A.show (d) method.
  • System.out.println (a2.show (b)); The A2 is declared as a type A, there is no exact matching method in a, a is transformed to a, the A.show (a) method is called, the A2 actual type is B, type B is covered by the a.show (a), and the B.show (a) method is run.
  • System.out.println (A2.show (c)); The A2 is declared as a type A, there is no exact matching method in a, the C-up is transformed to a, the A.show (a) method is called, the A2 is the actual type B, the type B overrides A.show (a), and the B.show (a) method is run.
  • System.out.println (A2.show (d)); The A1 is declared as a type A, there is an exact match in a method, the A.show (d) method is called, the A2 is the actual type B, and the Run method is the B.show (d) method.
  • System.out.println (b.show (b)); B is declared as type B, there is a fully consistent method in B, the B.show (b) method is called, B is the actual type B, and the Run method is B.show (b). This is the biggest difference from a2.show (b).
  • System.out.println (B.show (c)); B is declared as type B, there is no fully consistent method in B, the transformation from C up to B, the B.show (b) method is called, b the actual type is B type, and the Run method is B.show (b).
  • System.out.println (B.show (d)); B is declared as type B, there is a fully consistent method in B, the B.show (d) method is called, B is the actual type B, and the Run method is B.show (d).
In general, the JVM is not familiar with the mechanism of method invocation, causing the problem is not understood, Java Foundation is still bad.

Java polymorphic/Overloaded methods--a discussion caused by a problematic code

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.