The polymorphism of Java

Source: Internet
Author: User

1, polymorphic definition: Refers to allow different classes of objects to respond to the same message.

2, the realization of polymorphic technology: Dynamic binding, is to judge the actual type of the referenced object during execution, and call its corresponding method according to its actual type.

3: The role of polymorphism: eliminate coupling between types.

4: polymorphic example: Play lol, we all know qwer what the even recruit, but you press the ENTER key, and then press these qwer will trigger a different event, just pure qwer, no special effects.

Three prerequisites for polymorphism:

1, to have inheritance;

2, to have a rewrite;

3. The parent class reference points to the subclass object.

Remember the last one, you can relate to remember the previous. The parent class can point to subclasses, which must be inherited; Why does the parent need to point to subclasses? This is because the subclass overrides the method of the parent class.

But you only use one object, that is, the parent object, which points to a different subclass, you can invoke the specific method of implementing the various subclasses.

The benefits of polymorphism can be naturally elicited here:

1, flexible: embodies the flexible and diverse operation, improve the use of efficiency.

2, simplification: Unified use of an object to call different subclasses, you can simplify the application software code writing and modification process.

There are other benefits as well:

3. Replaceable

4. Extensibility

5, interface.

Implementation of polymorphism in Java: interface implementation, inheriting the parent class for method overrides, and method overloading in the same class.

The polymorphism of Java

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.