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.
Rewrite rule two: The parameter 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.
Rewrite rule three: 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.
Rewrite rule four: The override 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.
Note: This limitation is only for checking exceptions, and the runtime exception runtimeexception and its subclasses are no longer in this restriction.
Rewrite rule five: You cannot override a method that is identified as final.
Rewrite Rule vi: If a method cannot be inherited, you cannot override it. such as the Private method
Java Overwrite rewrite rules