The third most characteristic of Java--the understanding of polymorphism

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.