JAVA polymorphism is introduced by shallowness and depth

  • 2020-04-01 01:28:44
  • OfStack

What is polymorphism?

         

Polymorphism can be divided into two types:

(1)     Compile-time polymorphism (design-time polymorphism) : method overloading.

 

(2)     Runtime polymorphism: the JAVA runtime system's decision to choose which method to call based on the type of instance calling the method is called runtime polymorphism. (we talk a lot about runtime polymorphism, so polymorphism is basically runtime polymorphism.)

 

Three necessary conditions for the existence of runtime polymorphism:
Inheritance (including the implementation of the interface);
Two, to rewrite;
The parent class reference points to the subclass object.

 


--------------------------------------------------------------------------------

 

Detailed explanation:

 

Runtime polymorphism: a. runtime polymorphism refers to the process defined in the reference points to a specific type and b. through the reference variable making the method call is not sure when programming, but during the program is run to determine, that is, a reference variable STLL can point to which class instance object, the reference variable from method calls the method to realize exactly is which class, must can decide during the program is running.

 

1. The specific type of the reference variable defined in the program sequence is uncertain (that is, the instance object of which class a reference variable will be inverted).
 

Example:

Drive method (Vehicle class Vehicle) {}

The & # 8226; OneDriver. Drive (new car ())
The & # 8226; OneDriver. Drive (new bus ())
The vehicle variable cannot determine which subclass instance to use.

 

1. The method call made by the reference variable is not programmatically certain (the method call made by the reference variable is actually a method implemented in which class).
 

Example: Cut method call for chef, gardener, barber. Persion. Cut ().

 


--------------------------------------------------------------------------------

 

 

Benefits of polymorphism:

1. Substitutability. Polymorphic pairs of existing codes are replaceable. For example, the polymorphic class Circle works, as does any other circular geometry, such as a Circle.

2. Extensibility. Polymorphism is extensible to code. Adding new subclasses does not affect the polymorphism, inheritance, or operation of other features of existing classes. In fact, new subclasses are easier to get polymorphic functionality. For example, it is easy to add the polymorphism of sphere class on the basis of polymorphic of cone, half cone and half sphere.

3. Interface-ability. Polymorphism is realized by superclass providing a common interface to subclasses by means of method signature, which is perfected or overridden by subclasses. See figure 8.3. Two interface methods, computeArea() and computeVolume() to achieve polymorphism are defined by superclass Shape in the figure. Subclasses such as Circle and Sphere perfect or override these two interface methods in order to implement polymorphism.

4. Flexibility. It embodies the flexible and diverse operation in the application and improves the use efficiency.

5. Simplicity. Polymorphism simplifies the process of coding and modifying application software, especially when dealing with the computation and operation of a large number of objects.

 

 

Practical application:

Combined with the use of configuration files, contact the Spring framework, use reflection, dynamic call classes, at the same time without modifying the source code, directly add new classes and modify the configuration files, do not need to restart the server can extend the program.

 


--------------------------------------------------------------------------------

 

Summary:

Use a reference of the parent class type to point to a subclass object that invokes methods and variables defined in the master class. Variables cannot be overridden. If a method in a parent class is overridden in a subclass, the method in the subclass will be called when the method is called.

                Note that in special cases, if the method parameter list called by the parent class reference is not defined, the parent class is called to look up the parent class, if it is not found, the type is forced up the parameter type in the parameter list, the specific priority from high to low is as follows:

This. Show (O), super. Show (O), this. Show ((super)O), super. Show ((super)O).

 


--------------------------------------------------------------------------------

 

Classic pen questions (mixed reload and rewrite) :

(I) related classes


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...{} 

 

(2) question: what is the output of the following?

              A1 = new A();
              A2 = new B();
              B B = new B();
              C, C = new C();
              D mu D mu = new D mu ();
              System. The out. Println (a1) show (b));     1.
              System. The out. Println (a1) show (c));     2.
              System. The out. Println (a1) show (d));     3.
              System. The out. Println (a2) show (b));     (4)
              System. The out. Println (a2) show (c));     5.
              System. The out. Println (a2) show (d));     6.
              System. The out. Println (b.s how (b));         All landowners
              System. The out. Println (b.s how (c));         today
              System. The out. Println (b.s how (d));         Pet-name ruby      

(3) answer

                          (1)     A and A
                          (2)     A and A
                          (3)     A and D
                          (4)     B and A
                          (5)     B and A
                          6     A and D
                          All landowners     B and B
                          Today     B and B
                          Pet-name ruby     A and D

(4) analysis

              Easier to understand, generally not wrong. Is a little confused, why the output is not "B and B "? !!!!! Let's review polymorphism.

              Runtime polymorphism is one of the most powerful mechanisms for code reuse in object-oriented programming, and the concept of dynamics can also be described as "one interface, multiple methods." The foundation of Java's implementation of runtime polymorphism is dynamic method scheduling, which is a mechanism for calling overloaded methods at runtime rather than at compile time.

              Rewriting method Overloading and Overriding Overloading is a Java polymorphism of different performance. Rewrite the Overriding is one of the polymorphic between parent and child, Overloading Overloading is a class a polymorphism in performance. If a method defined in a subclass and its parent class have the same name and parameters, we say that this method be rewritten (Overriding). When an object in a subclass USES this method, it invokes the definition in the subclass, to which the definition in the parent class is "masked." If multiple methods with the same name are defined in a class that either have different number of arguments or have different parameter types, it is called Overloading of the method. Overloaded methods can change the type of return value but also have different parameter list.

              When a superclass object references 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 overridden by the subclass. (but if you force the superclass to subclass, you can call methods that are added to the subclass but not to the superclass.)

              All right, so that's all for review and back to business! In fact, the priority of method calls is involved here, with the order of priority being: this.show(O), super-.show (O), this.show((super)O), super-.show ((super)O). Let's see how it works.

              (4), for example, a2. The show (b), a2 is A reference variable, type A, is this for a2, b is an instance of b, then it into the class A for the show (b obj) method, is not found, then to A super (the superclass), and A not super class, therefore turned to the third priority this. The show (super) O, this is still an a2, here for b, O (super) O (super) b or A, so it into class A to find the show (A obj), the method of class A with this method, However, since a2 refers to an object of class B, and B overwrites the show(A obj) method of A, it finally locks to the show(A obj) of class B, and the output is "B and A ".

              Again today, for example, b.s how (c), b is A reference variable, type b, this is b, c is an instance of c, so it is to find A show in class b (c obj) method, is not found, turned to the super class b A find, inside A also have no, so also turned to the third priority this. The show (super) O, this is b, for c, O (super) O (super) c or b, so it to b inside to look for the show (b obj) method, found that due to the reference b is an object of class b, So lock directly to show(B obj) of class B, output "B and B ".

              Following the above method, other results can be obtained correctly.

              The question goes on. Now let's look at how the above analysis process reflects the meaning of the blue font sentence. It says that when a superclass object references 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 overridden by the subclass. Let's take a2. Show (b).

              A2 is A reference variable of type A, which refers to an object of type B, so it is up to B to decide which method to call. Therefore, B's show(B obj) should be called to output "B and B ". But why are the results from the previous analysis inconsistent? ! The problem is that we don't want to ignore the second half of the blue font, which specifically states that the method to be called must be defined in a superclass, that is, a method that is overridden by a subclass. Is show(B obj) in B defined in super class A? No! Let alone be covered. In fact, this sentence hides a message: it is still determined by the priority of the method call. It found the show in class A (A obj), if the subclasses B does not cover the show (A obj) method, then it is called A show (A obj) (due to inheritance. A, B, although not override this method but he inherited from the superclass A this method, in A sense, or call the method determined by B, just method is realized in A); Now subclass B overrides show(A obj), so it ends up locking into B's show(A obj). That's what that sentence means.


Related articles: