Yesterday saw a post about polymorphism, referring to the reply to the understanding of the people, joined some of their own views, collated out for your reference, not necessarily completely correct, welcome everyone to criticize correct.
(i) Related classes
Class A...{
Public String Show (D obj)...{
Return ("A and D");
}
Public String Show (A obj)...{
Return ("A and a");
}
}
Class B extends A...{
Public String Show (B obj)... {
Return ("B and B");
}
Public String Show (A obj) ... {
Return ("B and A");
}
}
Class C extends B ... {}
Class D extends B ... {}
(b) Question: What are the following output results?
A a1 = new A ();
A A2 = new B ();
b b = new B ();
c C = new C ();
D d = new D ();
System.out.println (A1.show (b)); ①
System.out.println (A1.show (c)); Ii
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)); ⑨
(c) The answer
①a and A
②a and A
③a and D
④b and A
⑤b and A
⑥a and D
⑦b and B
⑧b and B
⑨a and D
(iv) Analysis
①②③ better understand, generally will not be wrong. ④⑤ is a bit confused, why is not the output of "B and B" it?! Let's go back to polymorphism.
Runtime polymorphism is one of the most powerful mechanisms for object-oriented programming code reuse, and the concept of dynamic can also be said to be "one interface, multiple methods". Java's implementation of runtime polymorphism is based on dynamic method scheduling, which is a mechanism for calling overloaded methods at runtime rather than at compile time.
The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism. Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked". If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, which is called a method overload (overloading). The overloaded method is to change the type of the return value.
when a Superclass object reference variable refers to a subclass object, the type of the referenced object rather than the type of the reference variable determines which member method to call, but the method that is called must be defined in the superclass, that is, the method covered by the quilt class. (However, if you force a superclass to be converted to subclasses, you can call methods that are newly added in the subclass and that are not available in the superclass.) )
All right, let's get this over with. In fact, this involves the priority of the method invocation, with the priority being high to Low: This.show (O), Super.show (O), This.show ((Super) O), Super.show ((Super) O). Let's take a look at how it works.
For example ④,a2.show (b), A2 is a reference variable, the type is a, then this is A2,b is an instance of B, so it went to class A to find show (b obj) method, not found, so to a super (superclass), and a no superclass, So go to the third priority This.show (Super) O, this is still A2, here O is B, (super) O that is (super) B is A, so it goes to class A to find the method of show (a obj), Class A has this method, But since A2 refers to an object of Class B, B overrides the show (a obj) method of a, so it eventually locks to show (a obj) of Class B, and the output is "B and A".
Another example is ⑧,b.show (c), B is a reference variable, type B, then this is B,c is an instance of C, so it to Class B to find Show (C obj) method, not found, instead of to the B's superclass a inside find, a inside also not, so also go to the third priority This.show ( (Super) O), this is B,o C, (super) o i.e. (super) C is B, so it finds the show (b-obj) method in B, found that because B refers to an object of Class B, it is directly locked to show (b obj) of Class B, and the output is "B and B".
According to the above method, the other results can be obtained correctly.
The question goes on, now let's see how the above analysis process reflects the meaning of the words in the blue font. It says that when a superclass object references a variable that references a subclass object, the type of the referenced object rather than the type of the reference variable determines which member method to call, but the method that is called must be defined in the superclass, that is, the method covered by the quilt class. Take A2.show (b) for a while.
A2 is a reference variable, type A, which refers to an object of B, so the meaning of this sentence is B to decide which method to invoke. Therefore, the show (b obj) of B should be called to output "B and B". But why is it inconsistent with the results from the previous analysis?! The problem is that we don't overlook the second half of the blue font, which is particularly important: the method that is called must be defined in the superclass, that is, the method covered by the quilt class. Does the show (b obj) inside B have a definition in super Class A? No! That's not to mention being covered. In fact, this statement hides a message: it is still determined by the priority of the method invocation. It finds show (a obj) in Class A, and if subclass B does not overwrite the show (a obj) method, then it calls the show (a obj) of a (because B inherits a, although it does not overwrite this method, but inherits this method from superclass a, in a sense, The method that is called is determined by B, except that the method is implemented in A); now subclass B overrides Show (a obj), so it eventually locks to show (a obj) of B. This is what the meaning of the sentence is.
In-depth understanding of Java polymorphism [go]