polymorphism
Ask for compliments and thank your boss every day.
Welcome to pay attention to the public number: in fact, it is an Aries. Dry goods continue to be updated at……
1. Substance (more official answers)
Polymorphism refers to the fact that when system A accesses the services of system B, system B can provide the services through multiple implementations, all of which are transparent to System A.
2. Give examples to understand
1. The animal class; Subclasses: cat and dog; The parent class method cries() is overridden by the two subclasses.
Now, there’s a function that you can use to make the sound of an animal, and there are many different kinds of animals, so you just write:
public void animalCries(a) animals{
a.cries()
}
Copy the code
Call directly when animal sounds are needed:
Animals a =newThe cat (); animalCries(a); Animal b =newThe dog (); animalCries(b);Copy the code
You don’t have to change the original code. At compile time, the variable a is identified as the animal object declared in the code, and the program executes to see whether the animal a variable refers to a cat or a dog object. Dynamic binding.
conclusion
- Upcast: The declared variable type is the parent class, and the object referenced by the variable is a subclass. (Looks to be the father, a closer look is the son)
- For reference objects, the Java compiler treats them according to the type at which they are declared, and the Runtime Java virtual machine treats them according to the object they actually refer to.
- In the runtime, the binding of methods and member variables in reference types is regular:
- Dynamic binding: binding of instance methods (depending on the type of object referenced by the variable)
- Static binding: Static methods and member variables (see what type the variable is declared)
Daily begging
- If you found this article helpful, please “read/retweet/like /star”, thanks
- If you find a better or different idea, please feel free to comment on it in the comments area and discuss and modify it together. Thank you very much
Welcome to continue to pay attention to the public account: “Is actually white sheep”