Taste the glamour of Java subtype polymorphism

Source: Internet
Author: User

Summary:

Java programmers often use object polymorphism to make it easy to invoke appropriate methods in the right place, which is amazing. This approach is implemented through inheritance mechanisms. However, a rigorous experiment can make it clear and reveal that it is more appropriate to understand polymorphism as a type-related concept than to explain the inheritance mechanism. This understanding can help programmers to better use polymorphism.

------WM. Paul Roggs

The term "polymorphism" comes from Greek, meaning "multiple forms". Most Java programmers think of polymorphism as an object's ability to invoke the correct version of the method. Nevertheless, this perspective of implementation leads to the magical function of polymorphism, rather than simply thinking of polymorphism as a pure concept.

Polymorphism in Java is always polymorphism of subtypes. Almost mechanically produced some polymorphic behavior, so that we do not consider the types of problems involved. In this paper, we study a kind of object-oriented viewpoint, and analyze how to separate the behavior of the object and the behavior that the object will behave. Aside from the notion that polymorphism in Java is inherited, we can still feel that the interface in Java is a set of object sharing implementations that do not have common code.

Multi-State classification

Polymorphism is a common concept in object-oriented languages. Although we often confuse polymorphism, there are actually four different types of polymorphism. Before we begin the detailed discussion of the formal subtype polymorphism, let's look at the polymorphism in common object-oriented objects.

Luca Cardelli and Peter Wegner, author of the article "on understanding Types, Data abstraction, and polymorphism", Article reference resource link) divides polymorphism into two categories----specific and generic----four small categories: mandatory, overloaded, parametric and contained. Their structure is as follows:

In such a system, polymorphism shows many forms of ability. Generic polymorphic references have a large number of objects of the same structure type, and they share common characteristics. A particular polymorphism involves a small number of objects that do not have the same characteristics. The four polymorphic states can be described as follows:

Mandatory: A method of implicitly doing a type conversion.

Overloaded: Uses a marker as multiple meanings.

Parameters that provide the same action for different types of parameters.

Contains: An abstract operation in which a class contains a relationship.

I'll give you a brief introduction to these polymorphic forms before I tell you about subtype 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.