Java: Java rewrite overloaded with distinction

The most obvious difference is: override exists only in the parent class and subclass , overloading is present in a class .

 

Specific differences are as follows:

First, rewriting (override)

override is rewritten (overwritten) a method to achieve different functions. Is generally used when the subclass inherits the parent class, override the parent class (re-implement) .

Rewrite (cover) rules:

1, rewrite method of parameter list must be exactly the same method is overridden , but otherwise could not be called to rewrite overloaded.

2, a method of rewriting the access modifier must be greater than the access modifier is rewritten method (public> protected> default> private ).

3, a method of rewriting the return value must be consistent with the method of return to be rewritten ;

4, a method of rewriting the exception must be consistent thrown exception was thrown and the method of rewriting, or is a subclass ;

5, the method can not be rewritten Private , or only in its subclasses define a new method, there s no override it.

6, static methods can not be overridden method is non-static (compile error).

Second, the overloaded ( overload )

Overload present class constructor is defined different parameters, the general method is implemented in a number of overloaded for a class, the same names and forms of various parameters of these methods .

Overloading rules:

1, when a heavy load only, various parameters implemented in the form by the same method name . It may be different types of parameters, number of different parameters, a different order of parameters (parameter types must not the same);

2, can not access, return type, exceptions thrown overloaded ;

3, exception type and number of methods will not affect overloaded ;

 

Article reprint to: https://www.jianshu.com/p/c69aa573b93f

 

 
 
 
 
 

Guess you like

Origin www.cnblogs.com/nhdlb/p/12162886.html