Further understanding of the concept of polymorphism in Java

  • 2020-04-01 04:06:18
  • OfStack

There are two types of polymorphism:
1) compile-time polymorphism
For multiple methods with the same name, if one of the methods with the same name can be determined at compile time, this is called a compile-time polymorphism.
2) runtime polymorphism
If it is not determined at compile time and only at run time can it be determined which of the multiple methods with the same name will be executed, this is called runtime polymorphism.

Method coverage shows two kinds of polymorphism. When the object gets an instance of this class, it is compile-time polymorphism; otherwise, it is run-time polymorphism. For example:
XXXX x1 = new XXXX(parameter list); // object gets an instance of this class, which is of the same type as the instance it refers to
XXX xx1 = new XXX(parameter list);
X1. ToString (); // compile-time polymorphism, which executes the methods of class XXX.
Xx1. ToString (); // compile-time polymorphism, a method that performs XXXX class coverage.
XXXX is the parent class of XXX.
Since subclass objects are both superclass objects, and there is assignment compatibility between superclass objects and subclass objects, superclass objects can be assigned to subclass objects. For example,
XXXX x2 = new XXX(parameter list); // the parent object gets the instance of the subclass, which is the parent object
X2. ToString (); // runtime polymorphism
 
If x2 is declared as a parent object but gets an instance of the subclass XXX, then x2.tostring () performs the superclass method or the subclass override method.
There are two situations:
Depends on whether the subclass overrides the superclass method. If the subclass overrides the superclass method, the subclass method is executed.
If there is no override, the parent method is executed.
At compile time, based solely on the class to which the object belongs, the system cannot determine which class's methods should be executed, only the runtime can determine, so this is runtime polymorphism.
A superclass object cannot execute all subclass methods, but only those subclass methods in the superclass that declare the \ subclass override.

Java polymorphic implementation
Java polymorphism, like c++, is achieved through dynamic binding, or runtime binding. When a method referenced by an object is called, the compiler does not know whether the reference refers to an object of the type described when the variable is declared or to a subclass of that type. So the compiler cannot bind to a specific method for this call. Only runtime type recognition in Java (RTT) binds to specific methods at runtime

Rewriting methods and overriding methods overloading overloading is a Java polymorphism of different performance. Rewrite the overriding is a kind of polymorphism between parent and child classes, overloading overloading is a class of polymorphism.

Give a specific example:

   


 class People { 
  public String toString() { 
   return "I am a people!"; 
  } 
  
  public void eat() { 
  }; 
  
  public void speak() { 
  }; 
 } 
  
 class Boy extends People { 
  public String toString() { 
   return "I am a boy!"; 
  } 
  
  public void fight() { 
  }; 
  
  public void speak() { 
  }; 
 } 
  
 class Girl extends People { 
  public String toString() { 
   return "I am a girl!"; 
  } 
  
  public void sing() { 
  }; 
  
  public void speak() { 
  }; 
 } 
  
 public class TestToString { 
  public static void main(String args[]) { 
   People p = new Girl(); 
   System.out.println(p.toString()); 
  } 
 } 

The running result is:


I am a girl!

P is a reference to People, but the Girl toString method is still called at runtime because it is a Girl object


In-depth understanding of Java polymorphism
Statement, learning examples from other students here, the original link: http://blog.csdn.net/thinkghoster/article/details/2307001

The test subject


 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 { 
   
 } 
  
 public class Main { 
  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(a1.show(b)); // 1 
   System.out.println(a1.show(c)); // 2 
   System.out.println(a1.show(d)); // 3 
   System.out.println(a2.show(b)); // 4 
   System.out.println(a2.show(c)); // 5 
   System.out.println(a2.show(d)); // 6 
   System.out.println(b.show(b)); // 7 
   System.out.println(b.show(c)); // 8 
   System.out.println(b.show(d)); // 9 
  } 
 } 

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 

parsing
I started this problem and got all 4, 5, 6, and 9 wrong because I didn't have a good understanding of Java polymorphism, which is explained here

First, it is important to understand overrides and overrides, which include not only the same function names, but also parameter types and return value types

When a superclass object references a variable to a subclass object, the type of the referenced object rather than the type of the referenced variable determines whose member method is invoked, but the method must be defined in the superclass, that is, the method that is subclassed.

Then, let's analyze these problems again

Question: do you think B overrides parent A's show method? If I rewrite it, how many did I rewrite?

Answer: overwrite, overwrite one, public String show(A obj), why doesn't public String show(B obj) overwrite the superclass method, easy, because the argument types are different

Example analysis
After looking at the above analysis, let's also analyze two examples:

A, a2. The show (b) :

A2 is A reference variable of type A and b is an instance of b. First, I looked for show(B obj) in class A, and I didn't find it. Therefore, the method of show(A obj) was found in A. However, as an object of class B referenced by a2, B overwrote the method of show(A obj) of A, so it was finally locked to show(A obj) of class B, and the output was "B and A".

Second, a2. The show (c) :
A2 is A reference variable of type A and b is an instance of b. First, look for show(C obj) in class A. So we go to the super class of A, and A doesn't have A super class, so we go to A.this((super)C),(super)C is B, and by this point, this a2. Show (C) becomes a2. Show (B), and a2. Show (B) has been analyzed to be output "B and A", so this is output "B and A".



Related articles: