Java polymorphism detailed illustration (very classic example) __java

Source: Internet
Author: User

(i) Related class 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");           &NBSP}            public string show (a obj) ... {                  return   ("B and a");            }   }   Class c extends  b ... {}    Class d extends b ... {}  

(ii) 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));   ②           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 not wrong. ④⑤ is a bit confused, why the output is not "B and B" ... Let's review polymorphism first.

Run-time polymorphism is one of the most powerful mechanisms for object-oriented programming code reuse, and the concept of Java polymorphism can be said to be "an interface, multiple methods." Java implements Run-time polymorphism based on dynamic method scheduling, which is a mechanism for invoking overloaded methods at run time rather than at compile time.

The overridden overriding and overloaded overloading of methods are different manifestations of Java polymorphism. Overriding overriding is a manifestation of polymorphism between parent classes and subclasses, and overload overloading is an expression of polymorphism in a class. If you define a method in a subclass with the same name and parameters 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 invoked, and the definition in the parent class is "masked". If multiple methods with the same name are defined in a class, either with a different number of parameters or with different parameter types, they are called overloads of the Method (overloading). The overloaded method is to change the type of the return value. The overridden overriding and overloaded overloading of methods are different manifestations of Java polymorphism. Overriding overriding is a manifestation of polymorphism between a parent class and a subclass, and overload overloading is an expression of Java polymorphism in a class. If you define a method in a subclass with the same name and parameters 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 invoked, and the definition in the parent class is "masked". If multiple methods with the same name are defined in a class, either with a different number of parameters or with different parameter types, they are called overloads of the Method (overloading). The overloaded method is to change the type of the return value.

When a superclass object references a variable referencing a subclass object, the type of the referenced object, rather than the type of the reference variable, determines whose member method to invoke, but the invoked method must be defined in the superclass, that is, the method overridden by the quilt class. (But if you force a superclass to subclass, you can invoke a method that is not in the subclass of a new addition that is not a superclass.) )

Okay, let's go over here and get to the point. In fact, this involves the precedence of method calls, from highest to lowest precedence: 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, type A, then this is A2,b is an instance of B, so it finds show (b obj) method inside Class A, not found, and then to a super (superclass) to find, and a does not have a super class, So go to the third priority This.show ((Super) O), this is still A2, where O is B, (Super) O is (super) B is A, so it's looking for show (a obj) in Class A, and Class A has this method, But since A2 refers to an object of Class B, B overwrites the show (a obj) method of a, so it eventually locks into show (a obj) of Class B, and the output is "B and a".

Another example is ⑧,b.show (c), B is a reference variable, the type B, then this is B,c is an instance of C, so it went to Class B to find Show (C obj) method, not found, and then to B's super Class A inside find, there is no, so also go to the third priority This.show ( (Super) O), this is B,o C, (Super) O is (super) C is B, so it went to B to find show (b obj) method, found, because B refers to a Class B object, so directly locked to Class B's show (b obj), the output is "B and B."

According to the above method, you can get the other results correctly.

The question continues, now let's look at how the analysis process above reflects the meaning of the word blue. It says: When a superclass object refers to a variable referencing a subclass object, the type of the referenced object, rather than the type of the reference variable, determines whose member method to invoke, but the invoked method must be defined in the superclass, that is, the method overridden by the quilt class. Let's take A2.show (b) for a while.

A2 is a reference variable, the type A, which refers to an object of B, so the meaning of this sentence is B to determine which method is invoked. So you should call B's show (b obj) to output "B and B". But why is it inconsistent with the results of the previous analysis? The problem is that we do not ignore the second half of the blue font, which specifically indicates that the called method must be defined in the superclass, that is, the quilt cover method. b the show (b obj) is defined in super Class A. No. That's not to mention being covered. In fact, this statement hides a message: it is still determined according to the precedence of the method invocation. It found show (a obj) in class A, if subclass B does not overwrite the show (a obj) method, it calls show (a obj) of a (because B inherits a, although it is not overridden, but inherits this method from superclass a, in a sense, or by B to determine the method of invocation, only the method is implemented in A; now Subclass B covers Show (a obj), so it eventually locks to show (a obj) for B. That's what the meaning of that sentence is, and here we can clearly understand the polymorphism of Java.


Reprint: Blog Park

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.