As Java's third major feature-polymorphic, we must be familiar with, but for this abstract concept is always a bit vague, think in Java has done a lot of explanations in this chapter, specifically explained the three questions.
1. What is the problem with polymorphism? 2. What is polymorphic? 3. polymorphic Extensions
What problem does polymorphism solve?
Polymorphism solves the decoupling problem of type. Polymorphism allows us not to focus on what the specific type is, and the polymorphic design is to let us forget about the type. Because we don't have to focus on specific types, we can reuse a lot of code, and we can use a piece of code to solve similar problems. This is a big part of our development process, and this decoupling reduces the use of each of our layers. In general, the problem of multi-state solution is the decoupling problem of type. In thought, it is to let us forget the type.
What is polymorphic?
When I say this question, people may subconsciously think that I want to carry out some very abstract, very conceptual explanation, the actual I want to explain is not these conceptual things, these direct Baidu can. So back to the chase, what is polymorphism? Polymorphism actually has some other term, I prefer one of these, run-time bindings.
In explaining the word, let's first look at the bindings. Bindings are divided into pre-run bindings and run-time bindings. A pre-run binding is a program that knows what type of method to use when the program is not bound. In contrast, run-time binding is the type of method that the program does not know about before it runs, only the runtime knows the type of method used. This also achieves the effect of not having to know the specific type.
The overall mechanism of Java is to use runtime bindings. In addition to attributes and private and static methods.
Polymorphic expansion
We know that polymorphism can only control the methods of the parent class, and the way to extend it to subclasses is that it cannot be manipulated, but we will certainly encounter this situation. Then at this point we can no longer use the upward conversion, using a downward conversion. But there is a problem, that is, we do not know this type is not the type of your conversion, there will be a conversion error problem, this is not safe in C + +, but there is a mechanism in Java, is to convert the downward conversion must be forced to check, so that you can achieve security control, If you do not report a classcastexception error.