Perhaps everyone's Java's polymorphism, traceable, discontinued style, you must also be very clear about protected and private everyone, but these bindings, often make a lot of confusion, here, I will give an example, and everyone may find that this article is still very meaningful for you: Example There are two class classes. There may be a confusing place I will explain later. A is a parent class, B inheritance A and implement a ProtectedTest (Object Obj) method. As shown below: B.java Source code: package cnorg.matrix.test; import cnorg.matrix.test.a; / ** *
Title: protect, private and upcasting p> *
description: email: chris@matrix.org.cn p> *
Copyright: Matrix CopyRight (C) 2003 P> *
Company: Matrix.org.cn p> * @author chris * @version 1.0, who use this example pls remain the declare * / public class B extends A {protected int protectedb = 0; protected int protectedab = 0; protected Void protected {system.out.println ("in B.ProtECtedTest (Object):" Obj);}} A.java Source code: package cn.org.matrix.test; import cn .org. Matrix.test.b; / ** *
Title: protect, private and upcasting p> *
description: email: chris@matrix.org.cn p> *
Copyright: Matrix Copyright (c) 2003 p> *
Company: Matrix.org.cn p> * @Author Ch ris * @version 1.0, who use this example pls remain the declare * / public class A {protected int protecteda = 0; protected int protectedab = 0; private void privateTest () {System.out.println ( "in A.privateTest ( ) ");} Protected void protectedtest (object obj) {system.out.println (" in A.ProtaceTedTest (Object): " Obj);} protected void protectedtest (String str) {system.out.println (" in A.Protected: " Str);} public static void main (string [] args) {// test a a a a 1 =
New a (); a1.priVatetest (); // test b string hellostr = "hello"; object helloobj = hellostr; b b1 = new b (); a a2 = b1; // What happened here? Confusion 1 b1 = a1; // Compiling errors, confusing 2 b1. Privatetest (); // Compiling error, confusion 3 b1.protectedTest (Helloobj); // Output result? Confusion 4 b1.protacectedTedTest (Hellostr); // Compile Error, confusion 5 A2.ProtectedTest (Helloobj); // Output result? Confusion 6 A2.ProtectedTest (Hellostr); // Output result? Confusion 7?}} Below, I will explain every confusion: confusion 1 : Here is actually the automatic tracery of the subclass to the parent class A. Here A2 actually points to a B type object. We usually use this: a a2 = b1, this means that it means to point A2 points to a type B object - here is B1. In Java About cross-class references, there are two rules to remember: 1. If A is a reference to class A, then A can point to one instance of class A, or a subclass of class A. 2. If A It is a reference to interface A, then A must point to an instance of a class that implements interface A. So, according to these two rules, we do not understand what A A2 = B1 in the example is. Confusion 2: A A2 = B1 is ok, but why is B1 = A1 not? Here, we can still use the above two rules, we can see that B1 is a reference to class B, A1 is neither class B instance It is not an example of subclass of class B, so it has a compilation error in direct B1 = A1. If such conversion is required, we can do this: B1 = (b) A1; for mandatory conversion, it is traceable Style. In Java, the top-up model is automatically carried out, but the discontinuation is not, it is necessary to define the forced forced. Confusion 3: b1. Privatetest (); Compilation does not pass? This is obvious, you can review Private definition: Private domains and methods can only be defined to be accesses for this domain or method. So, here, B1 cannot access A method privateTest (), even if B1 is an example of a subclass of A. Take See the example below : Public class a {private int two (int i) {return i;}} class test extenship a {public static void main (string [] args) {system.out.println (A.TWO (3));}} System.out.println (A.TWO (3)); This line is compiled, obviously because the private method cannot be accessed outside of this class. PROTECTEDs are different, we review the protected definition: The protected domain or method can only be accessed by the class itself, the subclass of the class, and the class in the same package.
Below is an error using protected example: package cnorg.matrix.test; public class protectedTest {protected void show () {system.out.println (I am in protected method ");}}}}} import cn.org. Matrix.test. *; public class test {public static void main (string [] args) {protectedTest Obj = new protectedtest (); obj.show ();}} You will get "show () HAS because of access issues PROTECTED Access In Test.protaceTedTest "error message. Confusion 4: b1.protectedTedTedTest (Helloobj); output" in b.protectedTest (Object): ... "Why is this? Why is the JVM can determine how to output B? Not a method of A? This is related to the JVM's running mechanism. We mentioned that A1 is a reference, but points to an instance of a B type. Here, if JVM is based on the type of reference Here is a way to define which method of calling, then it should be a protectedtest (Helloobj) that call A. It is actually not the case, because JVM dynamic compiletable power, JVM will decide which Method is called in Run-Time, not In Compile Time. That is called Late-Binding (Compile-Time). Confusion 5: b1.protectedTest (Hellostr); Why do you have compilation errors? He can call class B ProtectedTedTest (Object obj) method, put the Hellostris shape into an object, it is OK .. Or trace the shape to a then call a protectedtest (Hellostr) method. Oh, the root of the problem is here, since there are two options What should JVM should choose? This uncertainty if it is handed over to JVM to dynamically determine the uncertainty .. Although Java is in other places There are also similar situations, such as the uncertainty caused by the cycle definition of Static variables, however, JVM still solves this problem in the compilation phase. So, we will encounter compilation errors in this step: "Reference to protectedtest IS Ambiguous; Both Method ProtectedTest (java.lang.string) in mytest.a and method protected in mytest.b match at line 46. Here, we encountered an explicit Reference Ambiguous error However, sometimes, implicit Reference Ambiguous is often more dangerous. Here, I will give an example: the source code of the parent class: public super {private void test (int i, long j); {system.out .println (i "and" j);}}} Sub-class source code: public sub {private void test (long j, int i); {system.out.println (i "and" j);}} Subcords and parent classs are used with the same name, the parameter type is different. In this case, compile can be passed. But if you use the following code in another class: SUB SB = New Sub (); Sb.Test (100, 3000);
You will encounter compilation errors because there is no certainty pointing to the 3000 type, so it causes the REFERENCE AmbIGUOUS 'error. Confusion 6: A2.ProtectedTest (Helloobj); output results are: "in b.protectedTest (Object) .." "After the above explanation, I will know why you will have these two output results: A2.ProtectedTest (Helloobj); because of the jvm Late-binding, call Class B when run-time Method, although A2 is just a reference type of a parent class A during compilation. Confusion 7: A2.ProtectedTedTest (Hellostr); why this will output "in A.ProtaceTedTest (Object) ...". Why not compile errors here? Why B1. Protectedtest (Hellostr) will be wrong and A2. PROTECTEDTEST (Hellostr) will be wrong? I call A2.Equals (b1) and A2 == B1 results are TRUE? But why is this error here? Here, this problem is the most critical, and it is the reason why we put them in the final answer. First, review equals () and == in Java concept, remember to have a SCJP topic: Title: What describes the following description. A. Equals () method determines whether the reference value points to the same object. B. == The operator determines whether the contents and types of two separate objects are consistent. C. Equals () method Returns true only when the content of the two objects. D. Class File Rewriting Method Equals () Returns true when the contents and types of the two separate objects are consistent. The answer is AD, strictly, this question is uncertain, because the equals () method can be overloaded, but if the new class does not rewrite equals (), the method is at the same object at two variables Return true. In fact, Java is also recommended to use the equals () method to determine whether the contents of the two objects are as described in the equals () method of the String class, determining whether the contents of the two String objects are the same. And the unique condition of the operator returns TRUE is that the two variable points to the same object. Here, we will no longer discuss the differences and concepts of equals () and ==. We only need to know that in our example, whether equals () and == are all meaning - whether the reference value points to the same object (because we have not rewritten equals ()). Obviously, we are in progress A2 = B1. After this step, the A2 and B1 are pointing to the same object.
Since pointing to the same object, why do you want to treat A2 and B1? Why does A2 do not compile errors, and B1 is to meet a Reference Ambiguous error? We now look jvm specification in the passage:. "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 pointer to a handle that is itself a pair of pointers: one to a table containing the methods of the object and a pointer to the Class object that represents the type of the object, and the other to the memory allocated from the Java heap for the object data "actually. Say: In the Java virtual machine, the reference to the class instance is a pointer to a handle. This handle is a pointer: a pointer points to a table, in fact, there are two pointers: a pointer points to one included The object's method table, the other pointing to the class object; the other pointer points to a piece of memory from the Java pile to allocate memory space. So, what happened in A2 = B1? In fact, when A2 = B1, there is still two handles, A2, and B1, but A2 and B1 have the same block of data memory blocks and different function tables. So when A2.ProtECtedTest (Hellostr), JVM will find the protectedtest (String Str) method from the A2's function table, but B1.ProtectedTest (Hellostr) will compile errors because JVM is not found in the function table of B1. At that, Ambiguous appeared on the back of its own shape or the argument. This is also our exact explanation of this issue. The interpretation of this problem is over, if you want to discuss this article related content, you can go: http://blog.9cbs.net/chensheng913/archive/2004/10/23/148309.aspx comments .