Java Experience Talk (2. Understanding of Java Polymorphism)

Polymorphism is one of the important features of object-oriented, I try to explain Java polymorphism in the simplest way:

To properly understand polymorphism, we need to clarify the following concepts:
・Defining types and actual types
・Overloading and overriding
・Compile and run

The actual type is the type following the new keyword.

Overloading occurs at compile time and is determined by the definition type.
Overriding happens at runtime and is determined by the actual type.

To determine which method of which class is called by a.fun(b), you can go through the following steps:
1. Use the inheritance rules to determine all the methods of each class, including the methods of the parent class;
2. Use the overloading rules , determine the method of defining the type corresponding to the compilation phase;
3. Use the rewriting rule to determine the method of the actual type corresponding to the running phase;
Note: If the rewriting rule cannot find a method with the same name and the same parameter and the same parameter, the parent will be called. A class with the same name as a method with the same reference. Nothing to do with overloading rules.

Let's do a classic problem, the code is as follows:

 1 class A {
 2     public String show(D obj)...{
 3         return ("A and D");
 4     } 
 5     public String show(A obj)...{
 6         return ("A and A");
 7     } 
 8 } 
 9 class B extends A{
10     public String show(B obj)...{
11         return ("B and B");
12     }
13     public String show(A obj)...{
14         return ("B and A");
15     } 
16 }
17 class C extends B...{} 
18 class D extends B...{}

 

Question: What is the following output?

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)); ⑨

 

Analysis:
Step 1: Use inheritance rules to determine all the methods owned by A and B; (C, D omitted)
A:
--show(D obj)→"A and D"
--show(A obj)→"A and A"
B:
--show(D obj)→"A and D"
--show(B obj)→"B and B"
--show(A obj)→"B and A"

Step 2, apply the overloading rule, the calling relationship is as follows: (determine which method to call)
① a1.show(b)⇒A.show(B→A)
② a1.show(c)⇒A.show(C→B→ A)
③ a1.show(d)⇒A.show(D)
④ a2.show(b)⇒A.show(B→A)
⑤ a2.show(c)⇒A.show(C→B→A)
⑥ a2.show(d)⇒A.show(D)
⑦ b.show(b)⇒B.show(B)
⑧ b.show(c)⇒B.show(C→B)
⑨ b.show(d )⇒B.show(D)
step 3, on the basis of step 1, continue to apply rewrite rules, determine the calling relationship, and apply the method of step 1: (determine which class to call)
① a1.show(b)⇒A .show(A)⇒A.show(A): output "A and A"
② a1.show(c)⇒A.show(A)⇒A.show(A): output "A and A"
③ a1. show(d)⇒A.show(D)⇒A.show(D): output "A and D"
④ a2.show(b)⇒A.show(A)⇒B.show(A): output "B" and A"
⑤ a2.show(c)⇒A.show(A)⇒B.show(A): output "B and A"
⑥ a2.show(d)⇒A.show(D)⇒B.show( D): output "A and D"
⑦ b.show(b)⇒B.show(B)⇒B.show(B):输出"B and B"
⑧ b.show(c)⇒B.show(B)⇒B.show(B):输出"B and B"
⑨ b.show(d)⇒B.show(D)⇒B.show(D):输出"A and D"

答案:
① 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

Copyright statement: The copyright of this tutorial belongs to java123.vip, and any form of reprinting and citation is prohibited.

The original post was published in: http://www.cnblogs.com/java123vip/p/9010373.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325913030&siteId=291194637