1, polymorphism is an important concept in object-oriented programming, which allows an interface or parent class reference to point to multiple actual types and can call corresponding methods according to the actual types. Specifically, polymorphism can be divided into two categories: compile-time polymorphism and run-time polymorphism.
2. Compile-time polymorphism, also known as static polymorphism, is determined in the compilation stage and is realized by overloading and rewriting. Overloading means using the same function name in the same class, but the types or numbers of parameters are different. At compile time, decide which function to call according to the type and number of parameters. Rewriting is to define the same method in a subclass as the function signature in the parent class.
3. Runtime polymorphism, also known as dynamic polymorphism, is determined at runtime, and it is realized by referencing subclass objects from the parent class. In Java, if the parent class references a subclass object, when calling the method referenced by the parent class, it will decide which method to call according to the actual object type.
Polymorphism
1. Animals: Animals are a very common example of polymorphism. There are different kinds of animals, such as dogs, cats, birds and so on. They all belong to different classes. Although they can all walk, run and fly, their behaviors and performances are quite different. For example, dogs can run fast, cats can climb high and birds can fly far.
2. Cars: Cars are also a polymorphic example. Cars have different brands and models, such as BMW, Mercedes-Benz and Audi. They all belong to different categories. Although they can be used as a means of transportation, they are very different in appearance, performance, driving experience and so on.
3. Graphical interface: Graphical interface is also an example of polymorphism. Different operating systems and software applications have their own interface styles and design methods, such as Windows, MacOS, Android and so on. Although they can be used to display text and pictures, their interface elements, interaction methods and operation methods are quite different.