Basic knowledge of Java method overloading and method of rewriting the difference

Overload a design method (overload):
overload defined method is in the same class, there is a method of a method of allowing one or more of the same name, as long as they can be different from the parameter list.

Method overloading role: shielding the same function method due to the different parameters caused by a different method name.

Method overloading judgment principle: "the same two different"
two same: the same, the same method name;
a different: Method list of the different parameters (parameter type, number of parameters, parameter order);
as long as the parameter type, number of parameters, parameter order there is a different argument list is different.

Note: The method of method overloading and regardless of the value of the return type, but generally require a consistent return value type.
Name list of parameters and parameter does not matter, method overloading and parameter does not matter.

Principle II method overridden (with two small one large): Override
together:
① instance method signature must be the same. (Method signature method parameter list = name + Method)
two small:
② subclass method return type is a parent class method returns the same type or of its subclasses.
Subclasses may return a more specific class.
Same type ③ exception subclass thrown exception types declared method of the parent class and method declarations thrown or subclass.
 subclass declared method throws exception than or equal to the parent class declaration method throws an exception type;
 subclass method can declare parent class belonging to a plurality of simultaneously throw method declaration throws a subclass of class (except a RuntimeException type) ;
big:
access method ④ subclass parent class is larger than or equal to the access method.
private modified method does not inherited by subclasses, covered by the concept does not exist.

Mortal blow is determined whether the override method: @Override tags: if the method is a method override, the label affixed on the prior method, compiler, otherwise, compiler errors.
The only way there is the concept of coverage, the field does not cover.
Method to solve the coverage problem: when one parent does not comply with the behavior of a subclass of the particular features of this case subclasses need to re-define the method of the parent class, and override methods body.

Third Method overloading and method overriding (method overrides) difference:
method overloading: Overload
method overrides: Override
itself both had nothing to do, just because the name like.
Method overloading: Overload
role: to solve . the same class, different methods were the same functionality problem
since it is the same function, then the name of the method should be the same as
the rules: two of the same or different.
similar, the same method name, different methods of parameter list (parameter type parameter . the number, order of parameters)
method overrides: override (rewrite method thereof)
action: after solving subclass inherits the parent class, a method may be a parent class does not satisfy the specific features subclass, in this case need to the method defined in subclasses and override the method body.
rule: with two small, one large.
with: parent class and subclass signatures are identical, it is recommended that: the definition of a direct copy of the parent process to the sub-class class, and then rewrite method thereof, OK.

Published 99 original articles · won praise 2 · Views 2628

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105116914