Method overrides are also known as overrides, and overrides are subclasses that redefine the methods of the parent class.
Rewrite:
1. The overridden method must have the same method name, argument list, and return value type as the original method (the return value type after Java SE5 can be a subtype of its type)
2. The overridden method cannot be a final type, because the final type cannot be overridden
3, the overridden method cannot be private, because private cannot inherit, and inheritance is the precondition of rewriting
4, the overridden method cannot be static, if the method in the parent class is static, but the method of the subclass is not static, but the other two methods satisfy the overriding condition, then a compilation error occurs, and vice versa. If a method in a subclass and a parent class is static, and the override condition is met, the override still does not occur because the static method matches the reference of the static method and the class at compile time.
5. The overridden method cannot be less accessible than the original method
6. The scope of the exception thrown by the override cannot be greater than the original method
7. Overrides are occurring at run time, and the JVM will make a decision when the code is run.
Method overloads are called overloads, which are cases where two or more method names in the same class have the same name but different parameters
Overload:
1. Method names must be the same
2, parameter list must be different (number of parameters, type, order of different)
3. The return type of the method can be the same or different
4. Only return value types are not enough to be overloaded with methods
5, overloading occurs at compile time, because the compiler can choose which method to use based on the parameter type
The difference between overrides and overloads:
1. Overrides can only be used for subclasses overriding the parent class, while overloading is used for all methods in the same class
2. The overridden parameter list must be the same, overloaded parameter list must be different
3. Override requires that the return value type must be consistent or its subclass, overloading does not require
4, overriding access to methods and throwing exceptions have special requirements, and the method of overloading does not have this limitation
5. Methods of the parent class can only be rewritten once by the same subclass, and a method can be overloaded many times in all classes
6, rewrite is run-time polymorphism, overload is a compile-time polymorphism
What does method overrides (overriding) and method overloads (overloading) in Java mean?