Polymorphism, java Polymorphism

Source: Internet
Author: User

Polymorphism, java Polymorphism

When writing code, we always see such an instantiation method: Animal mimi = new Cat (); but why does Animal have a Cat instead of a Cat mimi = new Cat () it looks a little clearer. Let's talk about why today.

What is polymorphism?

First, let's give you a concept: the interface is implemented in a variety of different ways, that is, polymorphism.

In fact, just like the above Animal mimi = new Cat ();, it means to dynamically bind it. For example, when I declare it, it is an Animal called mimi, but when the code runs, it automatically points the secret to the Cat () class to implement the Cat method.

Implementation of Dynamic binding (polymorphism)

As shown in, when we declare c, we place the c subtitle in the computer memory.HeapBut put the declared cat in the stack, we will just define the class currently declared during the declaration, just to identify the New rewrite method. The corresponding implementation code is found only when running.

As for the part of the Code, because the first time I was exposed to polymorphism, I wrote some, here it is no longer repeated, directly give the address, you can directly open it to see the http://blog.csdn.net/lovemenghaibin/article/details/21871785

The role of polymorphism:

1. The application does not need to write function calls for each derived class, but only needs to process the abstract base class. This greatly improves the reusability of programs.
2. The function of the derived class can be called by the method of the base class or the referenced variable, which is backward compatible and can improve scalability and maintainability.
 
Polymorphism is one of the main differences between object-oriented programming and process-oriented programming. What is polymorphism? I remember one of the articles in CSDN: "The child is different" polymorphism means the same processing method can be used to handle different situations.

Summary

Think about it. If there is no polymorphism, we need ten classes when there is an animal. It looks normal, but we will find that they have something in common when writing code, putting it on the code is a lot of repeated code, but with inheritance and polymorphism, it is different. We can abstract the same part or similar part, and then implement different places, although the class has an abstract class, our code is less, it looks clear, and the scalability is enhanced. If you want to add a uniform function, you can also program the interface.


What is polymorphism?

For example, there is a parent class superClass, which has two sub-classes subClass1 and subClass2. SuperClass has a method
Func (), both subclass override this method. So we can define a superClass reference obj to point it to a subclass object, such as superClass obj = new subClass1 (); then we call obj. the func () method is dynamically bound, that is, the actual func () method of obj, that is, the func () method of subClass1. Similarly, you write superClass obj = new subClass2 (); obj. func () actually calls the func () method of subClass2. This is because the subclass overrides the parent class method and then uses the parent class reference to point to the subclass object. dynamic binding is performed when the method is called, which is a polymorphism. Polymorphism plays a very important role in program extension. For example, if you want to have another subClass3 instance, there will be fewer things you need to change. If you use the configuration file, you will not be able to change the source code.

What is polymorphism?

For example, there is a parent class superClass, which has two sub-classes subClass1 and subClass2. SuperClass has a method
Func (), both subclass override this method. So we can define a superClass reference obj to point it to a subclass object, such as superClass obj = new subClass1 (); then we call obj. the func () method is dynamically bound, that is, the actual func () method of obj, that is, the func () method of subClass1. Similarly, you write superClass obj = new subClass2 (); obj. func () actually calls the func () method of subClass2. This is because the subclass overrides the parent class method and then uses the parent class reference to point to the subclass object. dynamic binding is performed when the method is called, which is a polymorphism. Polymorphism plays a very important role in program extension. For example, if you want to have another subClass3 instance, there will be fewer things you need to change. If you use the configuration file, you will not be able to change the source code.

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.