The concept and characteristics of polymorphism

concept

Polymorphism (Polymorphism) refers to the characteristics that the same object has many different forms of expression or performance capabilities in different scenarios. In object-oriented programming, polymorphism means that after inheriting the parent class, the subclass can rewrite the parent class method and implement the method in its own way, so that the same behavior has different manifestations.

For example, define an animal class Animal, which contains a method eat(), and then define two subclasses Cat and Dog, which inherit the Animal class and rewrite the eat() method. The eat() method of the Cat class is to eat fish , the eat() method of the Dog class is to eat meat. At this time, if we define a variable animal of the Animal type and assign it a value of new Cat() or new Dog(), then whether we call animal.eat() or animal.run(), the corresponding subclass will actually be called The method rewritten in , that is, Cat's eat() or Dog's eat() method. This is the embodiment of polymorphism, the same method call, but different subclasses are implemented in their own unique ways, achieving the purpose of code reuse and scalability.

example

Here is an example of polymorphism in Java code that implements the Animal, Cat, and Dog classes described above:

public class Animal {
    
    
    public void eat() {
    
    
        System.out.println("Animal is eating something.");
    }
}


public class Cat extends Animal {
    
    
    @Override
    public void eat() {
    
    
        System.out.println("Cat is eating fish.");
    }
}


public class Dog extends Animal {
    
    
    @Override
    public void eat() {
    
    
        System.out.println("Dog is eating meat.");
    }
}


public class Main {
    
    
    public static void main(String[] args) {
    
    
        Animal animal1 = new Cat();
        Animal animal2 = new Dog();

        animal1.eat();  // 输出 "Cat is eating fish."
        animal2.eat();  // 输出 "Dog is eating meat."
    }
}

In the above example, the Cat and Dog classes both inherit the Animal class, and rewrite the eat() method in the parent class, and output different information according to different subclasses. In the main method of the Main class, two variables animal1 and animal2 of Animal type are defined, which are initialized with Cat instance and Dog instance respectively. When calling the eat() method, animal1 will call the eat() method in the Cat class and output "Cat is eating fish.", while animal2 will call the eat() method in the Dog class and output "Dog is eating meat." , which reflects the characteristics of polymorphism.

features

Polymorphism is an important idea in object-oriented programming, which has the following characteristics:

  1. Inheriting the parent class: polymorphism requires the existence of an inheritance relationship, and the subclass must inherit or implement certain methods of the parent class.
  2. Method rewriting with the same name: The subclass needs to rewrite the method in the parent class, and keep the method name, parameter list and return type all the same.
  3. Runtime performance: When calling this method, the method in which subclass to use is determined according to the type of the actual parameter. This binding method is also called late binding or dynamic binding, because it is not determined at compile time, but at runtime.
  4. Code reuse: Polymorphism can improve the code reuse rate, because subclasses can share the methods already implemented by the parent class, and there is no need to write repeatedly.
  5. Replaceability: Polymorphism makes the program have good replaceability. When the function needs to be modified, only the method in the corresponding subclass needs to be modified without modifying other codes.
  6. Extensibility: Polymorphism is conducive to adding new subclasses to extend functions, because they can achieve their own unique behavior by inheriting the parent class and rewriting the methods in it, without affecting other parts of the code.

In short, polymorphism is a very important object-oriented programming feature, which can improve code scalability, maintainability and reusability, and is one of the core concepts of object-oriented programming.

when to use

The best time to use polymorphism is usually when a parent class has some methods that need to be implemented by different subclasses, and all subclasses must have a uniform format and calling method for these methods.

Here are some scenarios where polymorphism is used:

  1. Processing object collection: When you need to apply a certain method to different types of objects in this collection, you can use polymorphism to call the corresponding method during the traversal process.
  2. Superclass Concurrent Operations: Polymorphism will handle asynchronous calls, event-driven program data flow, and concurrent operations in superclasses very well.
  3. Interface design: Polymorphism is also very suitable for use in interface design, because the code is extensible in this way, which is conducive to code refactoring and other generalization operations.
  4. Callback mechanism: Polymorphism can also be widely used in the callback system, that is, when some specified events or roles occur, the corresponding event handler or role manager is called through polymorphism.
  5. Implementation of business rule engine: Polymorphism also plays a vital role in the setting of business rules.
  6. Framework development: In the framework development phase, polymorphism is usually used to ensure the correct interaction between the framework's presentation experience and state, and to support various pluggable implementations.

In short, in most cases, if you need a flexible, scalable, and maintainable code implementation, and you need to ensure the simplicity and reuse of the code, then polymorphism is the best choice.

Guess you like

Origin blog.csdn.net/qq_45450889/article/details/131181063