Method overloading and rewriting
1. Method Overloading
Method overloading means that the same method in the class has different implementation forms, depending on the parameters specified by the caller. Different parameters are given, and different methods are called.
Note that the method name, Parameter order, and type constitute the signature of the method. In the class, the signature of each method must be unique, and the compiler is the root
The method signature is used to determine the method to be called, but the return type does not affect the method signature.
2. Method Rewriting
Method rewriting occurs only when the child class inherits the parent class. When the subclass inherits the parent class, we can directly use all the methods in the parent class, but sometimes the methods in the parent class cannot fully meet our needs, we can change the method of the parent class appropriately, which is called method rewriting.
Sometimes it is also called method overwrite by mistake (but it is not really covered, so it is somewhat far-fetched ).
We need to note that there are some differences between method rewriting and overwriting:
A. Method rewriting requires that the method signature be the same, while the method overloading requires that the method signature be different.
B. The return types of method rewriting must be consistent, while the method overload is not required.