Overload and Override the difference. Overloaded methods can change the type of return value?

Overload is overloaded meaning,

Override for coverage, which is rewritten.

Overload Overload represents the name of the same method for a plurality of the same class, but the parameter list of these methods vary (i.e., different number or type of parameters).

Rewrite Override representation subclass can be the name of a method and parameters parent class is exactly the same instance of an object created by a subclass when calling this method, invoking the method defined subclass, which is equivalent to exactly the same manner that the parent class definition to cover, object-oriented programming which is a manifestation of polymorphism.

When the parent class subclass covering, can only throw fewer exceptions than the parent, or the parent class is thrown exceptions thrown by the abnormal child, because subclasses can solve some of the problems of the parent class, the class can not be less than the Father there are more problems.

Access subclass method can not be less than the greater of the parent class.

If the parent class is private type, then the subclass is not restricted coverage exists, corresponds to a subclass adds new approach.

As for whether Overloaded methods can change the return type of the problem depends on what you want to ask are they going to do? The questions were very vague. If the parameter list of a few Overloaded methods are not the same, they are returnees may not be the same type of course.

But I guess you want to ask the question: If the argument list of two methods exactly the same, whether they can make the return value to achieve different overloads Overload.

This is not acceptable,

We can use reductio ad absurdum to illustrate the problem, because we sometimes call a method can not define variables return results that do not care about their return results,

For example, when we call map.remove (key) method, although the remove method returns a value, but we often do not receive variables defined in the returned results, the class have exactly the same name and parameter list two ways at this time hypothesis , just a different type of return, java programmers can not determine which method they going to want to call, because it can not be judged by the type of return results.

override can be translated to cover, from the literal you know, it covers a method and rewrite them in order to achieve different effects.

For us, the most familiar is to achieve coverage of the interface method, the interface in general is just a statement of the method, and when we realize, we need all the methods declared in the interface implementation.

In addition to this typical usage, we may also cover the method of the parent class in a subclass in inheritance. In the coverage to pay attention to the following points:

Override features

1, a method of covering the mark must be exact match and mark method is covered in order to achieve the effect of coverage;

2, the return value of the method must return a consistent coverage and methods covered;

3 must be consistent abnormality abnormality, the method of covering thrown and thrown covered method, or is a subclass;

4, the method can not be covered by the private, or subclasses thereof defines a new method only, and not be overridden.

overload may be familiar to us, can be translated as overloading, it means that we can define some of the same name of the method, when defined by different input parameters to distinguish between these methods, then call, VM will be based on different parameters style, executed to select the appropriate method. In the heavy-duty use should pay attention to the following points:

Overload characteristics

1, in the use of heavy-duty only through different parameter style. For example, different types of parameters, number of different parameters, a different sequence parameter (of course, several types of parameters within the same method must be different, for example, may be fun (int, float), but not as fun (int, int ));

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

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

4, inheritance, if the access is a method in the parent class is priavte, then it can not be overloaded in a subclass, if defined, only defines a new method, without reaching the weight load effect.

to sum up:

the override (override cover) (occurs in the parent class and subclass)
1, method name, arguments, return the same value.
2, subclass method can not be reduced access to the parent class method.
3, subclass method can not throw more exceptions than the parent class method (methods subclass but may not throw an exception).
4 exists between the parent and child classes.
5, is defined as a final method can not be rewritten.

overload (overload, overload) (occur in the same method)
1, parameter types, the number, at least one sequence are not the same.
2, can not override the return value only different method name.

Guess you like

Origin blog.csdn.net/weixin_44517301/article/details/93131834