When it comes to Java overloads and overrides, we're all familiar with it, but I'm going to write this today.
Article topic:
A. What is overload
Two. What is overlay
Three. The difference between the two
Overload (Overload):
In a class, if two or more than two functions of the same name appear, they can be called the function overload as long as the number of their arguments, or the type of the parameter is different.
That is, when the function has the same name, just look at the argument list. Does not matter with the return value type.
When overloading is used, you need to be aware of:
1. When using overloads, you can only pass different parameter styles. For example, the different parameter types, the number of different parameters, the different parameter order.
2, the method's exception type and number does not affect the overload.
3, for the parent class, subclasses can overload a method with the same name as an inherited parent class, and if a method is priavte in the parent class, it cannot be overloaded on a subclass, and if defined, it simply defines a new method and does not achieve the overload effect.
Overlay (Override):
When an identical method appears in the child parent class, creating the subclass object runs the method in the subclass. As if the methods in the parent class were overwritten. So this is another feature of the function: overwrite.
When do I use overlay?
When a subclass inherits the parent class, the function content of the parent class needs to be modified, and it can be implemented by overriding
When overriding a method, note two points:
1. When a subclass overrides a parent class, it must be guaranteed that the permission of the subclass method must be greater than or equal to the parent class method permission to implement inheritance. Otherwise, the compilation fails.
2, cover, or both static, or not static. (Static can only be overridden by static or statically overwritten) YES
3. The final modified method is a definitive method and cannot be overwritten.
4. When a subclass overrides a method of a parent class, it can only throw fewer exceptions than the parent class, or it throws a child exception of the exception thrown by the parent class, because subclasses can solve some of the problems of the parent class and cannot have more problems than the parent class.
The difference between overloading and overriding:
1. The coverage of the method is the relationship between the subclass and the parent class, and is the vertical relation; The overload of the method is the relationship between the methods in the same class and the horizontal relationship.
2. Coverage can only be caused by one method, or by a pair of methods; The overload of a method is the relationship between multiple methods.
3. The override requires the same parameter list; The overload requires a different parameter list.
4. In the coverage relation, the method body is called according to the type of the object, and the overload relation is to select the method body based on the actual parameter list and the formal parameter list of the call.
The above is a small series for you to bring in-depth understanding of Java overload and cover all the content, I hope that we support cloud Habitat Community ~