Overriding and overloading are different manifestations of Java polymorphism.
Overriding is a manifestation of polymorphism between parent class and Child class, while Overloading is a manifestation of 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.
Is called the method overload ). Exceptions cannot be thrown through access permissions, return types, or other methods;
.
1. Override features
1. Mark of the covered Method Required Identifier of the method to be overwritten Exact match To achieve the coverage effect;
2. Return Value of the override method Required It is consistent with the returned result of the overwritten method, or its subclass;
3. The exception declaration thrown by the method has no effect on overwrite, that is, the subclass can declare any number or type of exceptions. (exception is a bit special)
4. The method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.
2. Overload features
1. When heavy load is used Only Use different parameter styles. For example, different parameter types, different parameter numbers, and different parameter Order (of course, several parameter types in the same method must be different, such as fun (INT, float ), but cannot be fun (INT, INT ));
2, It cannot be overloaded by access permission, return type, or thrown exception;
3. The method exception type and quantity do not affect the heavy load;