Ii. Java Object-oriented (9) _ Object-oriented--multi-state thought

Source: Internet
Author: User

2018-05-02

multi-State thought

Polymorphism is the ability of the same behavior to have many different manifestations or forms.

Polymorphism is the same interface that uses different instances to perform different operations:

Polymorphism is the embodiment of multiple forms of objects.

In reality, for example, we press the F1 key for this action:

  • If the current pop-up in the Flash interface is the help document as 3;
  • If the word help is currently popping up under word;
  • Windows Help and support pops up under Windows.

The same event occurs on different objects and produces different results.

--------------------------------------------------------------------------------------------------

For example, you are a wine God, have a unique feeling for wine. Some day home found a few cups inside are loaded with white wine, from the outside to see we can not know what this is what wine, only after drinking to be able to guess what kind of wine. You drink, this is Jiannanchun, drink again this is wuliangye, again drink this is alcoholic wine .... Here we can describe it as follows:

Wine A = Jiannanchun//At this time a represents the morphology of the Jiannanchun type

Wine B = wuliangye//At this time B represents the form of the Jiannanchun type

Wine c = Alcoholic wine//At this time C represents the form of the Jiannanchun type

...

What we are doing here is polymorphism. Jiannanchun, Wuliangye, alcoholic wine are the sub-class of wine, we just through the wine this one parent can refer to different subclasses , this is polymorphic-we can only run when we know the reference variable point to the specific instance object .

There are two types of objects (A/B/C):

  Compilation type: declares the type of variable, wine

  type of operation: True Type of object, Jiannanchun/Wuliangye/alcoholic wine

The compilation type must be the parent class of the run type (up) or the same as the run type (downward transition), and polymorphism is the parent of the run type of the compilation type.

In the drinking example above, the wine (Win) is the parent class, Jiannanchun (JNC), Wuliangye (wly), alcoholic wine (JGJ) is a subclass. We define the following code:

JNC a = new JNC ();

For this code we are very easy to understand is to instantiate a Jiannanchun object! But what about this?

Wine a = new JNC ();

Here we understand that a wine type of a, which points to an Jnc object instance, is defined here. Since Jnc is inherited from wine, Jnc can automatically move up to wine, so a can point to Jnc instance objects.

There is a great benefit in this, as we know in inheritance that subclasses are extensions of the parent class, that it can provide more powerful functionality than the parent class, and that if we define a reference type that refers to the parent class of a subclass, it can also use the subclass's powerful functionality in addition to referencing the generality of the parent class.

--------------------------------------------------------------------------------------------------------------- -------------------------------------

Conditions for the implementation of polymorphism:

1. Inheritance (classes and classes) or interfaces (interfaces and implementation classes)

2. Overwrite (rewrite)

3. Parent class reference to child class object (up Transformation)

principle: when a super (parent) class object references a variable that references a subclass object, the type of the referenced object rather than the type of the reference variable determines which member method to call (that is, the method by which the subclass object's type determines who is called ).

But the method that is called must be defined in the superclass, that is, the method covered by the quilt class.

--------------------------------------------------------------------------------------------------------------- -------------------

Characteristics of polymorphism:

The parent class reference points to the subclass object, which displays the specific subclass characteristics at run time.

A parent class reference to a subclass because it has been transformed upward, it can only access methods and properties that are owned in the parent class, and the reference is not available for methods that exist in the subclass and that do not exist in the parent class, although the method is overloaded.

If subclasses override some of the methods in the parent class, these methods must be called using the methods defined in the subclass (dynamic joins, dynamic calls).

Two, Java object-oriented (9) _ Object-oriented-multi-state thought

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.