A, one of the rewrite rules:
The overriding method cannot have a stricter access level than the overridden method limit. (but more broadly, such as the parent class method is the package access permission, the subclass's overriding method is public access.) )
For example: The object class has a ToString () method, it is always easy to forget the public modifier when we begin to rewrite this method, and the compiler will of course not miss any chance to teach us. The reason for the error is that there is no access modifier with the package access permissions, package access permissions than the public of course, so the compiler will error.
b, rewrite the second rule:
The argument list must be the same as the overridden method. Rewrite has a twin brother called Overload, that is, behind the appearance. If the parameters of the subclass method are different from those of the parent class, then you are mistaken, and that is overloaded, not rewritten.
C, rewrite the third rule:
The return type must be the same as the return type of the overridden method.
The parent class method A:void eat () {} Subclass Method B:int eat () {}, although the parameters are the same, but the return type is different, so it is not overridden. The parent class method A:int eat () {} Subclass Method B:long eat () {} The return type is compatible with the parent class, but the difference is different, so it is not overridden.
D, rewrite the rule four:
An overriding method cannot throw a new exception or check an exception that is more extensive than the check exception declared by the overridden method. However, you can throw fewer, more limited, or non-throwing exceptions.
1Import java.io.*;2 3 Public class Test {4public staticvoidMain (string[] args) {5Animal h =NewHorse ();6 Try {7 h.eat ();8}Catch(Exception e) {9 Ten } One } A } - - class Animal { thePublicvoidEat () - throws exception{ -System.out.println ("Animal is eating."); - Throw NewException (); + } - } + A class Horse extends animal{ atPublicvoidEat () - - throws ioexception{ -System.out.println ("Horse is eating."); - Throw NewIOException (); - } in}
In this example, the parent class throws a check exception exception, and the subclass that throws the IOException is a subclass of exception, which is a more limited exception than the overridden method, which is possible. If, in turn, the parent class throws IOException, the subclass throws a more general exception, then it is not compiled.
Note: This limitation is only for checking exceptions, and the runtime exception runtimeexception and its subclasses are no longer in this restriction.
E, rewrite the rules of the Five:
You cannot override a method that is identified as final.
F, rewrite the rule six:
If a method cannot be inherited, it cannot be overridden. More typically, the private method of the parent class.
The next meeting produces an interesting phenomenon.
1 Public class Test {2public staticvoidMain (string[] args) {3 //Animal h = new Horse ();4Horse h =NewHorse ();5 h.eat ();6 }7 }8 9 class Animal {TenPrivatevoideat () { OneSystem.out.println ("Animal is eating."); A } - } - the class Horse extends animal{ -Publicvoideat () { -System.out.println ("Horse is eating."); - } +}
This code can be compiled. It appears to violate the sixth rule, but it's actually a bit of a coincidence. The Eat () method of the animal class cannot be inherited, so the Eat () method in the horse class is a completely new method, not a rewrite or an overload, but a completely new method that only belongs to the horse class! This makes a lot of people confused, but it's not that
Difficult to understand. The main () method if this is the case: Animal h = new Horse (); Horse h = new Horse (); H.eat (); The compiler will give an error, why? The Eat () method of the horse class is public! Should be able to call Ah!
Keep in mind that polymorphism only looks at the methods referenced by the parent class, not the child class object.
"Java" Java method overwrite rules