Data abstraction, inheritance and polymorphism are the three main features of object-oriented programming language. Polymorphic, I think its role is to use the interface and implementation of the separation, improve the structure of the code, enhance the readability of the code. In some very simple cases, we may not use polymorphism to develop programs that meet our needs, but most of the cases, if not polymorphic, the code is extremely difficult to maintain.
In Java, talking about polymorphism is about binding a method call, and binding is associating a method invocation with the same method body. In the C language, the binding of a method (called a function in C) is implemented by a compiler, known in English as early binding (pre-binding), so it is natural to think of the corresponding late binding (late binding), which is commonly called run-time in Java. Binding (runtime bindings), I personally think this is more appropriate, the purpose of the runtime binding is to be able to judge the type of object when the code is running. A simple example illustrates:
Dynamic binding
Connecting a method call and a method body together is called binding. Depending on the timing of the binding, you can divide the bindings into "early binding" and "late binding." If you bind (done by the compiler and the linker) before the program runs, it is called early binding. If you are binding during a program run, called late binding, late binding is also known as dynamic binding or runtime binding. In Java, polymorphism is implemented by dynamic binding, where the Java virtual machine determines which method to invoke with the same name at run time.
Application of polymorphism
Because of polymorphism, a reference variable for a parent class can point to a different subclass object, and the corresponding subclass method is executed at run time based on the actual type of the object to which the parent reference variable points. Two distribution with polymorphism. Use polymorphism to design callback methods.
Java polymorphism
object-oriented programming has three features, namely encapsulation, inheritance, and polymorphism.
Encapsulation hides the internal implementation mechanism of the class so that it can change the internal structure of the class without affecting the consumer, while protecting the data.
inheritance is intended to reuse the parent class code, while preparing for the implementation of polymorphism. So what is polymorphism? The Overrides, overloads, and dynamic joins of the
method form polymorphism. Java introduces the concept of polymorphism, one of the reasons is that it is in the inheritance of the class and C + +, which allows multiple inheritance, which does give it a very powerful function, but the complex inheritance of the C + + developers have also brought greater trouble, in order to avoid the risk, Java only allow single inheritance, There is a is-a relationship between the derived class and the base class (that is, "cat" is a "animal"). Although this ensures that the inheritance relationship is simple and clear, but there is bound to be a large number of functional limitations, so Java introduced the concept of polymorphism to compensate for this shortcoming, in addition, abstract classes and interfaces are also important means to solve the single inheritance restrictions. At the same time, polymorphism is also the essence of object-oriented programming.
To understand polymorphism, you first need to know what is "upward transformation".
I defined a subclass cat, which inherits the animal class, and the latter is the parent class. I can pass
Cat C = new Cat ();
Instantiating a cat object is not difficult to understand. But when I define this:
Animal a = new Cat ();
What does that mean?
Very simply, it means I have defined a reference to the animal type, pointing to the new cat type object. Because Cat is inherited from its parent class animal, references to animal types can point to objects of the cat type. So what's the point of doing that? Because subclasses are an improvement and an extension to the parent class, the generic subclass is functionally more powerful than the parent class, and the attributes are more unique than the parent class.
An object that defines a reference to a parent class type that points to a subclass can use both the powerful functionality of a subclass and the commonality of the parent class.
Therefore, a reference to a parent class type can invoke all the properties and methods defined in the parent class, and it is helpless for a method that is not in the parent class defined in the subclass;
At the same time, a method in the parent class can be called by a reference to the parent class only if it is defined in the parent class and is not overridden in the subclass.
For a method defined in a parent class, if the method is overridden in a subclass, then a reference to the parent class type invokes this method in the subclass, which is the dynamic connection.
Java object-oriented-polymorphic