Java basics: Polymorphism (overloading and rewriting)
(1) domain and static methods
Remember"Only calls of common methods are polymorphism.".
The domain and static methods are not: the access to the domain has been resolved and bound during the compilation process. If a method is static, there is no polymorphism.
(2) The constructor has a criterion:
* Use the simplest method possible to make the object normal and avoid calling other methods as much as possible.
* Only the final methods that can be safely called in the constructor (private default final), because they cannot be overwritten.
(3) concept of polymorphism:
Polymorphism refers to the coexistence of different methods with the same name in a program.
The reason why these methods have the same name is that they have the same final function and purpose. However, when the same function is completed, different situations may occur, therefore, you need to define methods that contain different specific content to represent multiple specific implementation forms.
(4) Java provides two types of polymorphism mechanisms: overloading and rewriting.
Overriding and Overloading are different manifestations of Java polymorphism.
Overriding is a manifestation of the polymorphism between the parent class and the Child class, and Overloading is a manifestation of the polymorphism in a class.
="If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ).When a subclass object uses this method, the definition in the subclass is called. For it, the definition in the parent class is "blocked.
="If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called Overloading ).The Overloaded method can change the type of the returned value (that is, only different return values are not Overloaded ). Even if the parameter names are the same, as long as the types are different, they are overloaded even if they are mutually inherited.