What is polymorphism? What are the benefits of polymorphism? Where is polymorphism used?

Source: Internet
Author: User

What is polymorphism.

Concept: The same operation acts on different objects, can have different interpretations, produce different execution results, this is polymorphism. Simply put: the reference to the base class refers to the object of the subclass.

Why use polymorphism?

Reason: We know that encapsulation can hide implementation details, making code modular, inheritance can extend existing code modules (classes), and their purpose is to-code reuse. In addition to the reusability of code, it can also solve the problem of tight coupling in the project and improve the scalability of the program. The degree of coupling is between module modules, the relationship between code code, through the analysis of the system to decompose him into a child module, the child module to provide a stable interface, to reduce the system coupling degree of purpose, module interface between modules to use as much as possible to access the module, rather than arbitrarily referencing the other modules of the member variables.

What is the advantage of polymorphism.

There are two benefits:

1. The application does not have to write functional calls for each derived class, only the abstract base class must be processed. Greatly improve the reusability of the program. Inherited
2. The functionality of a derived class can be invoked by a method of a base class or by a reference variable, which is called backward compatibility, and can increase extensibility and maintainability. The real role of polymorphism,

Where polymorphism is used.

Can be used in the parameters of the method and in the return type of the method.

For the parameters in the method, consider the following example:

   Public abstract class Driver {public void run ()//Let subclasses run} class Sbenz:driver {public
        void Run () {Console.WriteLine ("Benz in run at 200 mph"); } class Jd:driver {public void run () {Console.WriteLine (' JD is running ... ')
        ;
        The class Person {private Driver Driver;
        Public person () {} public person (Driver Driver) {this.driver = Driver;
        public void Drive () {Driver.run ();
        } public void Setdriver (Driver Driver) {//use parameter polymorphism, then no matter what car can be bought this.driver = Driver;
            } static void Main (string[] args) {person p = new person ();
            JD JD = new JD ()//The first to buy a JD bar when you have no money p.setdriver (JD);
            P.drive ();
            Sbenz Benz = new Sbenz ()//have money to change the P.setdriver (Benz); P.dRive (); }

In the return type of the method, consider the following example:

In the example above, both JD and Benz are directly new to us. We can design a factory class that specializes in building cars

/**
* We use polymorphism in the return type of the method
* Driver can be abstract or interface, JD and Benz either inherit the class or implement the excuse

*/

public class Carfactory
    {public
        Driver factory (String carname)
        {
            if (carname.equals ("JD"))
            { return
                new JD ();
            }

            else if (carname.equals ("Benz"))
            {return
                new Sbenz ();
            }

            else
            {
                Console.WriteLine ("Contrast, not Serve");
                return null;}}}
    

This is actually a simple factory pattern in design mode!

The parameters of the type., when passing parameters, you can pass in any object of a class, as long as it is the object name of the corresponding class.
This is the application of polymorphism!

(with

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.