About the characteristics of Java polymorphism

Source: Internet
Author: User

Polymorphism--concept

a multi-state refers to a reference (type) A variety of states in different situations. It can also be understood that polymorphism refers to a method that is implemented in a different subclass by pointing to a pointer to the parent class.

There are two ways to implement polymorphism:1, inheritance, 2, interface

Polymorphism--Precautions:

1. Java allows a reference variable of the parent class to refer to an instance of its subclass (object)

Animal an=new Cat ();//This conversion is done automatically

inheritance is the basis for the realization of polymorphism. Literally, polymorphism is a type (all car type) that shows a variety of states (BMW's name is BMW, the price is 300000; the name of the Chery car is cheryqq, the price is 2000). Associating a method call with the principal (that is, an object or a class) to which the method belongs is called binding, which is both pre-binding and late-bound. Here's how they're defined:

1. Early binding: Binding before the program runs, implemented by the compiler and the linker, also called static binding. For example, the static method and the final method, note that this also includes the private method, because it is implicitly final.

2. Late binding: Binding according to the type of the object at run time, implemented by the method invocation mechanism, is also called dynamic binding, or run-time binding. All methods except pre-binding are late-bound.

Polymorphism is achieved on the mechanism of late binding. The benefit of polymorphism is that it eliminates the coupling between classes and makes the program easier to extend.

Final concept

Final Chinese meaning: The last, the final.

Final can modify variables or methods.

In some cases, the programmer may have the following requirements:

1. When you do not want a method of the parent class to overwrite (override) The quilt class, you can use the final keyword to decorate it.

2. When you do not want the value of a variable of a class to be modified, you can use the final modifier. If a variable is final, the initial value must be assigned, or a compilation error occurs.

3, when you do not want the class to be inherited, you can use the final decoration.

About the characteristics of Java polymorphism

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.