Polymorphism in Java

Source: Internet
Author: User

Source: http://www.cnblogs.com/mengdd/archive/2012/12/25/2832288.html

The concept of polymorphism

  polymorphic = = Late binding .

Do not interpret function overloading as polymorphic.

Because polymorphism is a run-time behavior, not a compile-time behavior.

Polymorphic: A reference to a parent type can point to an object of a subtype.

For example, Parent p = new Child ();

    When calling a method using polymorphic mode, first check if there is a method in the parent class, and if not, compile the error;

If so, then call the subclass of the same name method.

(note here that static statics methods are special cases, static methods can only inherit, cannot override override, and if the subclass defines a static method with the same name, it only acts as a hidden effect on the parent class method.) The version of the call with whom the reference is made . )

(Refer to Learning Links: http://docs.oracle.com/javase/tutorial/java/IandI/override.html)

If you want to invoke a method in a subclass that is not in the parent class, you need to cast the coercion, as in the example above, convert p to a reference to subclass child type.

  A method that does not exist in the parent class is not found when invoking the method with the parent class reference, because the parent class references the object to the child class. A downward type conversion is required to convert the parent class reference to a subclass reference.

Polymorphism in 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.