Related knowledge of polymorphism in Java

Source: Internet
Author: User

Example:

public class A {public String show (D obj) {return ("A and D");    Public String Show (a obj) {return ("A and a");    }}public Class B extends a{public String show (b obj) {return ("B and B");    Public String Show (A obj) {return ("B and A");        }}public class C extends B{}public class D extends B{}public class Test {public static void main (string[] args) {        A a1 = new A ();        A A2 = new B ();        b b = new B ();        c C = new C ();                D d = new D ();        System.out.println ("n" + a1.show (b));        System.out.println ("2--" + a1.show (c));        System.out.println ("3--" + a1.show (d));        System.out.println ("4--" + a2.show (b));        System.out.println ("5--" + a2.show (c));        System.out.println ("6--" + a2.show (d));        System.out.println ("7--" + b.show (b));        System.out.println ("8--" + b.show (c));          System.out.println ("9--" + b.show (d)); }}

The result of the operation is:

1--a and A2--a and A3--a and D4--b and A5--b and A6--a and D7--b and B8--b and B9--a and D

Here to see results 1, 2, 3 good understanding, starting from 4 began to be confused, for 4 why output is not "B and B" it?

First, let's take a look: 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 quilt class overrides the method. This statement is a generalization of polymorphism. In fact, the invocation of an object method in the inheritance chain has a priority: This.show (O), Super.show (O), This.show ((Super) O), Super.show ((Super) O).

Analysis:

From the above procedure we can see that a, B, C, D has the following relationship.

First we analyze 5,a2.show (c), A2 is a reference variable of type A, so this represents the A,a2.show (c), it found in Class A is not found, so to a super Class (super), because a no superclass (except object), so jump to the third level, That is This.show (Super) O, the superclass of C has B, a, so (Super) O is B, A,this is also a, where show (a obj) is found in a, and since A2 is a reference to Class B and Class B overrides show (a obj), Therefore, the show (A obj) method of subclass Class B is eventually called, and the result is B and A.

I can also confirm other answers in the same way.

The method has been found. But we still have a question here: 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, This means that the quilt class overrides the method. We use an example to illustrate what this sentence means: A2.show (b);

Here A2 is a reference variable, a type, it refers to the B object, so according to the above sentence means that there is a B to decide who to call the method, so A2.show (b) should call B in the show (b obj), the resulting result should be "B and B", But why is it different from the results of the previous operation? Here we ignore the following sentence "but the method called here must be defined in the superclass", so does show (B obj) exist in Class A? It doesn't exist at all! So this is not a valid sentence here? So is this the wrong sentence? Not too! In fact, this sentence also implies that this sentence: it still has to follow the inheritance chain to invoke the priority of the method to confirm. So it will find show (a obj) in Class A, and the method in Class B is called because B overrides the method, otherwise the method in Class A is called.

Therefore, the principle of polymorphic mechanism is summarized as follows: 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 quilt class overrides the method, However, it still needs to confirm the method based on the priority of the method invocation in the inheritance chain, which is: This.show (o), Super.show (O), This.show (Super) O, Super.show ((Super) O).

Related knowledge of polymorphism in Java

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.