Differences between Java rewriting and overloading, and between Java rewriting and overloading
Override)
Rewriting is the process that the subclass re-writes the methods allowed to access the parent class. The returned values and parameters cannot be changed.That is, the shell remains unchanged and the core is overwritten!
The advantage of rewriting is that child classes can define their own behaviors as needed. That is to say, sub-classes can implement the parent class method as needed.
The override method cannot throw a new check exception or a broader exception than the declared by the override method. For example, a method of the parent class declares an IOException check, but an Exception cannot be thrown when this method is rewritten because Exception is the parent class of IOException, only subclass exceptions with IOException can be thrown.
Method rewriting rules
- The parameter list must be exactly the same as that of the method to be overwritten;
- The return type must be exactly the same as the return type of the method to be overwritten;
- The access permission cannot be lower than the access permission of the override method in the parent class. For example, if a method of the parent class is declared as public, you cannot declare this method as protected if you rewrite it in the subclass.
- The member method of the parent class can only be overwritten by its subclass.
- Methods declared as final cannot be overwritten.
- Methods declared as static cannot be overwritten, but can be declared again.
- The subclass and its parent class are in the same package. Then, the subclass can override all the methods of the parent class except the methods declared as private and final.
- The subclass and the parent class are not in the same package. Therefore, the subclass can only override the non-final method declared as public and protected for the parent class.
- The method to be rewritten can throw any non-forced exception, regardless of whether the method to be rewritten throws an exception. However, a method to be rewritten cannot throw a new mandatory exception, or be more extensive than a mandatory exception declared by the method to be rewritten. Otherwise, it can.
- The constructor cannot be overwritten.
- If you cannot inherit a method, you cannot override this method.
Overload)
Overloading is a class with the same method name and different parameters. The return types can be the same or different.
Each overloaded method (or constructor) must have a unique list of parameter types.
Only constructors can be reloaded.
Reload rules
- The parameter list must be changed for the method to be overloaded (the number, type, or order of parameters are different );
- The method to be overloaded can change the return type;
- The access modifier can be changed for the overloaded method;
- The overloaded method can declare a new or wider check exception;
- Methods can be overloaded in the same class or in a subclass.
- The return value type cannot be used as the criteria for distinguishing heavy-duty functions.
| Differences |
Overload Method |
Rewrite Method |
| Parameter List |
Must be modified |
Cannot be modified |
| Return type |
Modifiable |
Cannot be modified |
| Exception |
Modifiable |
Can be reduced or deleted, and a new or wider exception cannot be thrown. |
| Access |
Modifiable |
Do not impose more stringent restrictions (can reduce the restrictions) |
Summary
Overriding and Overloading are different manifestations of java polymorphism. Rewriting is a manifestation of polymorphism between parent class and Child class, overload is a manifestation of a type of polymorphism.