Further understand the concept of polymorphism in Java

There are two kinds of polymorphism: 1) compile time polymorphism. For multiple methods with the same name, if it can be determined which one of the methods with the same name can be executed at compile time, it is called compile time polymorphism 2) Runtime polymorphism is called runtime polymorphism if it cannot be determined at compile time and only at run time can it be determined which of multiple methods with the same name is executed

Method coverage shows two kinds of polymorphism. When the object obtains an instance of this class, it is compile time polymorphism, otherwise it is run-time polymorphism, for example: XXXX X1 = new xxxx (parameter list)// The object obtains the instance of this class, and the object is consistent with the instance type it references XXX xx1 = new XXX (parameter list); x1. toString(); // Compile - time polymorphism to execute the methods of class XXX xx1. toString(); // Compile time polymorphism, the implementation of XXXX class coverage method XXXX is the parent class of XXX Because the subclass object is both the parent object and the assignment compatibility between the parent object and the subclass object, the parent object can be assigned as a subclass object For example, XXXX x2 = new XXX (parameter list)// The parent object obtains the subclass instance, and the subclass object is the parent object x2 toString(); // If the runtime polymorphic X2 is declared as a parent object but obtains an instance of the subclass XXX, then x2 Does tostring() execute the parent method or the subclass override method? This is divided into two cases: depending on whether the subclass overrides the parent method If the subclass overrides the parent method, the subclass method is executed; If there is no override, the parent method is executed At compile time, the system cannot determine which method of the class should be executed only according to the class to which the object belongs. It can only be determined at run time. Therefore, this is runtime polymorphism The parent class object cannot execute all subclass methods, only those subclass methods declared \ overridden by subclasses in the parent class

Java polymorphism is realized by dynamic binding or runtime binding, just like C + +. When a method referenced by an object is called, the compiler does not know whether the reference refers to the type object described when the variable is declared or the object of the subclass of that type. Therefore, the compiler cannot bind to a specific method for this call. Only through runtime type recognition (RTT) in Java can you bind to specific methods at run time

Overriding of methods and overloading of methods are different manifestations of Java polymorphism. Overriding is a manifestation of polymorphism between parent and child classes, and overloading is a manifestation of polymorphism in a class.

Give a specific example:

The operation result is:

P is a reference to people, but at runtime, because it is a girl object, the toString method of girl is called

For an in-depth understanding of Java polymorphism declaration, here we learn from the examples of other students. The original link is as follows: http://blog.csdn.net/thinkghoster/article/details/2307001

Test topic

answer

Analysis I started to do this topic. 4, 5, 6 and 9 all made mistakes because I didn't understand the polymorphism of Java well. Here's an explanation

First, we should deeply understand rewriting and overloading. Rewriting includes not only the same function name, but also parameter types and return value types

Secondly, deeply understand the sentence "when a superclass object references a variable and a subclass object, the type of the referenced object rather than the type of the referenced variable determines whose member method to call, but the called method must be defined in the superclass, that is, the method overridden by the subclass"

Then, let's analyze these topics

Question: do you think B has overridden the show method of parent class a? If so, how many?

Answer: rewritten, rewritten one, that is, public string show (a obj). Why doesn't public string show (b obj) override the parent method? It's very simple because the parameter types are different

Based on the above analysis, let's also analyze two examples:

1、 A2 show(b):

A2 is a reference variable of type A, and B is an instance of B. First, find show (b obj) in class A, but it is not found. So I went to the superclass of a, but a had no superclass, so I turned to A. This ((super) b), and (super) B was a, so I found the method of show (a obj) in A. However, as an object of class B referenced by A2, B rewrites the show (a obj) method of a, so I finally locked it to show (a obj) of class B, and the output is "B and a"

2、 A2 Show (c): A2 is a reference variable of type A, and B is an instance of B. First, find show (C obj) in class A, but it is not found. So I looked in the superclass of a, but a has no superclass, so I turned to A. This ((super) C), where (super) C is B. so far, this A2 Show (c) becomes A2 Show (b), and A2 Show (b) it has been analyzed that the output is "B and a", so here is also the output of "B and a"

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>