Speaking of overloading and overwriting in Java, everyone is familiar with it, but I will write this today.
Topic of this article:
1. What is overloading
2. What is coverage
3. The difference between the two
Overload:
In a class, if two or more functions of the same name appear, as long as the number of parameters or the types of parameters are different, it can be called overloading of the function.
That is, when the function has the same name, only look at the parameter list. It has nothing to do with the return value type.
Pay attention to when using overload:
1. When using overloading, you can only pass different parameter styles. For example, different parameter types, different parameters number, and different parameter order.
2. The exception type and number of methods will not affect overloading.
3. For the parent class, the child class can overload a method with the same name as the inheritance method in the parent class. If a method has access permission in the parent class and is priavte, then it cannot be overloaded in the child class. If it is defined, it will only define a new method and will not achieve the effect of overloading.
Override:
When exactly the same method appears in the child parent class, creating a subclass object will run the methods in the subclass. It seems like the method in the parent class is overwritten. So this situation is another feature of the function: overwriting.
When to use coverage?
When a child class inherits the parent class, the functional content of the parent class needs to be modified, it can be implemented through overrides
When covering the method, pay attention to two points:
1. When a subclass overrides the parent class, it is necessary to ensure that the permissions of the subclass method must be greater than or equal to the permissions of the parent class method to achieve inheritance. Otherwise, the compilation fails.
2. When covered, either they are static or they are not static. (Static can only cover static, or be covered by static) YES
3. The final modified method is a final method and cannot be overwritten.
4. When a child class overrides the parent class method, it can only throw fewer exceptions than the parent class, or throw child exceptions of the exception thrown by the parent class, because the child class can solve some problems of the parent class and cannot have more problems than the parent class.
Differences between overloading and overwriting:
1. The override of a method is the relationship between the subclass and the parent class, which is a vertical relationship; the overloading of a method is the relationship between the methods in the same class, which is a horizontal relationship.
2. Overwrite can only be caused by one method, or only by one pair of methods; overloading of a method is the relationship between multiple methods.
3. The coverage requirement parameter list is the same; the overload requirement parameter list is different.
4. In the overwrite relationship, the method body is called based on the type of the object; the overload relationship is selected based on the actual parameter table and the formal parameter table at the time of call.
The above is the in-depth understanding of the overloading and coverage of Java brought to you by the editor. I hope everyone can support Wulin.com more~