Java-Preliminary Understanding-tenth-polymorphism-Transformation 2

Source: Internet
Author: User

One.

Previous examples of lifting, Animal a=new Cat (); When the animal's reference points to the Cat object, the Cat object has two forms. Either the shape of the cat or the form of an animal. At this time, the Cat object will have polymorphism. This is an elevation when the parent class points to a child object. This is called upward transformation. Turned into an animal type. If you need subclasses to do this, you need to move down.

Now let's take another example to illustrate the transformation of polymorphism.

(This example is a bit of a problem, the subclass has two "lectures" method, the method body is not the same)

According to the truth, we are to create teacher Bi object, call the teacher method.

All of the above are early actions, and the polymorphic operations are described next.

in this case, we can explain the polymorphism more clearly, and the animal example of proportion is clearer. In the polymorphic operation of this example, the result of the compilation run is Java. I thought the output would be management, not the output of Java. Here to finish teacher and grandpa in two of the same function to do an introduction, Mr. Bi inherited the master, so that the same method subclasses covered the parent class. If the parent class is an abstract class, the same function will have one more abstract keyword, no method body, but the function in the subclass will still overwrite it.

The reason for the output of Java here is that although X is the type of grandpa, but in the memory diagram, the value of X is the subclass of the object Teacher's address code. So, the lecture in the X-Output is the class in the subclass object, which is Java, not the parent's teaching management.

The program has been modified to find that I understand wrong.

According to the previous understanding, there should be no fishing function, because X is essentially a subclass object, there is no fishing function in the subclass.

→ The understanding of inheritance is not deep enough, my sight is concentrated on the same function of the overlay. I thought the sub-class teacher had only two functions, lectures and movies. In fact, for everything in the parent class, as long as the subclass is inherited, there is also a subclass, but it is not written out, that is, the function of fishing, there are also sub-categories. If it is the same declared function, the method of the subclass function overrides the method body in the parent class function.

Then make a change to the program as follows

Added an X. Watch movie (); this sentence. My first feeling is that it can be run, but it's actually wrong. Or because the previous understanding is not deep enough right. The outside world has always treated X as a grandpa to see, coverage can not be seen, the extension of the sub-category is also not seen. Therefore, the outside world can not call the movie to see the function. ( see the movie function in X is real, how to use it?) Does it have to be converted to a subclass type ? )

If you want to see a movie, you can use the unique data of the sub-class. This requires a conversion of the data type.

The whole process down and that's it

In the process of multi-state and up-down transformation, there is one thing that does not change, from beginning to end, the subclass object is changing.

Keep in mind that the upward transformation has two effects, increasing extensibility and limiting the unique functionality of subclasses. When converting types down, be sure to prevent type conversion exceptions from occurring.

Java-Preliminary Understanding-tenth-polymorphism-Transformation 2

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.