Method coverage
In the class inheritance, the subclass can modify the method of inheritance from the parent class, that is, the subclass can create a method with different functions with the parent class, but it has the same name, return value type, and parameter list.
If a method is defined in the new class, the name, return value type and parameter list are just the same as the parent class, then the new method is called the old method covering the old method.
The parameter list is also called the parameter signature, including the type of parameter, the number of parameters, and the order of the parameter. As long as there is a difference, the parameter list is different.
The method of being covered can only be called through Super in the subclass.
Note: The coverage does not delete the method of the parent class, but it is hidden for the instance of the subclass and is not used for the time being.
Please see the following example:
Public Class Demo {Public Static Void Main (String [] ARGS) {Dog MyDog = New Dog ("Wing Circle"); mydog.say (); // The method of instances in the subclass calls the method in the subclass animal myanmial = new. Animal ("" walking circle online "); myanmial.say (); // The instance of the parent class calls the method of the parent class} Class Animal {String name; Public Animal (String name) {This.name = Name;} public void say () {system.out.println ("I am a small animal, my name is" + name + ", I will make a cry"); Inherited, call the public dog (string name) {super (name);} // cover the say () method public void say () {system.println ("I am a puppy, mine, my The name is " + name +", I will make a barking sound "));}}
Run results:
Copy code code as follows:
I am a puppy, my name is the walking circle, I will make a barking sound. I am a small animal. My name is online. I will make a call.
Methods covering principles:
The return type, method name, and parameter list of the cover method must be the same as the original method.
The coverage method cannot be worse than the original method (that is, the access permissions are not allowed to be reduced).
The coverage method cannot be thrown more abnormal than the original method.
The method of being covered is not the final type, because the method modified method cannot be covered.
The method of being covered cannot be private, otherwise it only defines a method in its sub -class and does not cover it.
The coverage method cannot be static. If the method in the parent class is static, and the method in the subclass is not static, but the two methods except this to meet the coverage conditions, then compile errors will occur; vice versa. Even though the methods in the parent and subclasses are static and the coverage conditions are met, it will still not cover, because the static method is to match the static method and class reference type when compiling.
Method heavy load:
The Java method has been explained before. Here, I emphasize that the methods in the Java parent and subclasss will participate in the heavy load. For example, one method in the parent class is Func () {...}, subclasses, subclasses One method is Func (int i) {...}, which constitutes the re -load of the method.
Different coverage and heavy load:
Method coverage requirements parameter list must be consistent, and the method of re -loading requirements parameters must be inconsistent.
Method coverage requirements The return type must be consistent, and the method has no requirements for this.
Methods can only be used to cover the parent class, and the method is overloaded to all methods in the same class (including the method inherited from the parent class).
Methods have special requirements for access permissions and abnormal throws, and the method has no restrictions in this area.
One method of the parent class can only be covered by the subclasses once, and one method can be reloaded multiple times in all classes.