In Java, polymorphism means the same behavior, different implementations.
Among them, polymorphism is divided into static polymorphism and dynamic polymorphism. The difference between the two is that the compiler can determine the final result of the run time, that is, compile time can determine which method to call, while the latter in the compilation period will not determine the effect of the operation, only after the run, according to the object of the binding, to determine the operation effect, that is, the running system to determine the object referred to by the Static polymorphism is mainly manifested in the overloading of methods and the use of separate methods, while dynamic polymorphism is embodied in dynamic binding and method rewriting.
Before you introduce the concept of dynamic binding, you need to understand the transformation technology first. Transformation is the conversion of a data type, which usually occurs when the data types are different on the left and right sides of the assignment symbol.
For basic data types, conversions of data types can be done automatically when a data type that represents a small data range translates to a data type that represents a large data range, such as an int type to a double type. Conversely, when a large range of data types is converted to a data type with a small range, we need to cast it with parentheses around the data type to be turned, and to complete the forced explicit conversion before the variable to be turned. However, it has to be pointed out that forcing data type conversions can result in loss of data precision, such as 3.14 of a double type converted to 3 of type int, and the precision of the decimal part 0.14 is lost.
For a reference data type conversion, there is a precondition for the conversion, which is not all types of reference data types can go, the conversion must occur in the class with the inheritance relationship. Similar to the basic data types, this reference data type conversion is divided into upward transformation and downward transformation. Because the reference data type is referred to by a reference to a specific object, when a type conversion actually occurs, it is simply a conversion of the type "reference", and the actual object does not change. The upward transformation is the assignment of a reference to the parent class object, so that the conversion is completed automatically and must succeed. The downward transition is to assign a reference to a subclass object (that is, the result of an upward transformation) to the parent class reference, at which point the subclass reference and the original subclass object must be the same (class reference to this class object), or the runtime will have a type conversion exception.
With the above concept of transformation, we can explain the dynamic polymorphism in detail: when there is a parent class reference on the hand, when calling the method overridden by the quilt class, only after it is run, depending on whether the reference actually points to that object (the parent class itself or a subclass), the final result of the method is determined.
If a dynamic polymorphic method is used to bind the parent class reference, then the only properties and behaviors that can be seen from the parent class reference that are allowed by the access modifier of the parent class are the problem, so how do you want to see the properties and behaviors that are unique to the subclass, and how do you determine the parent class reference binding is the Seed class object?
Using the instanceof keyword, you can solve this problem perfectly. The instanceof keyword is an operator that is used to determine if a reference is pointing to an object that is not an instance of a type, and if it returns a Boolean value of true, otherwise false, so that we also get a way to judge the specific type of reference that we bind to the parent class, At this point we can transform the parent class reference of the binding to the corresponding subclass reference, so that the subclass and behavior of the subclass can be recalled.
When two classes are bound to a relationship, that is, when the method in the class needs to be associated with the other class, the parent reference of the binding party can obtain better extensibility and improve the reusability of the code.
Java Polymorphism-code reusability