1. Overriding
Overriding overwrites a method to achieve different effects. (That is, the tone must be the same) for us, the most familiar coverage is the implementation of the interface method. In the interface, it is generally only the declaration of the method, and when we implement, all methods declared by the interface must be implemented. In addition to this typical usage, we may also overwrite the methods in the parent class in the subclass.
The main features of rewriting are:
1.The method name must be the same as the method to be rewritten. 2. The list of method parameters must be the same as that of the method to be overwritten. 3. The return type must be the same as the method to be overwritten. 4. The override method must provide greater permissions than the method to be overwritten. E.g. if the override method is protected void method (int I, Double J), the override method can be protected void method (int I, Double J) (generally ), or public void method (int I, Double J), but it cannot be private void method (int I, Double J) or the default package... 5. A new exception or a wider exception cannot be thrown. 6. The method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.
2. OverloadingOverloading means that we can define methods with the same name. We can define different input parameters to differentiate these methods (in different tone) and then call them again, JVM selects appropriate method execution (polymorphism) based on different parameter styles ).
The main features are as follows: 1. The method names for each overload are consistent. 2. The parameter list of each overload method is different (including one or more of the three parameters in the parameter type, number of parameters, and Parameter order ).
3. Any return type. (The return value of a method cannot be used to distinguish between overloaded methods .) 4. Any access controller. (You cannot distinguish between overloaded methods by their access permissions .) 5. You can throw your own exceptions, regardless of the overloaded method. (The overload method cannot be distinguished by the thrown exception .)