Override and overload differ, can the overloaded method change the type of the return value?

Source: Internet
Author: User

Overload is the meaning of overloading,

Override is to overwrite the meaning, namely rewrite.

Overloaded overload means that there can be more than one method with the same name in the same class, but the parameter lists of these methods vary (that is, the number of arguments or the type differs).

overriding override means that a method in a subclass can be exactly the same as the name and parameters of a method in the parent class, and when called by the instance object created by the subclass, the definition method in the subclass is called. This is equivalent to overwriting the exact same method defined in the parent class, which is also a representation of the polymorphism of object-oriented programming.

When a subclass overrides a method of a parent class, it can only throw fewer exceptions than the parent class, or a child exception that throws an exception thrown by the parent class, because subclasses can solve some problems with the parent class and cannot have more problems than the parent class.

The subclass method can only be accessed more than the parent class, and cannot be smaller.

if the method of the parent class is Private type, the subclass does not have an overlay limit, which is equivalent to adding a new method to the subclass.


As to whether the Overloaded method can change the type of return value, it depends on what you want to ask. The subject is very vague. If several Overloaded methods have different parameter lists, their type of returnees can of course be different.

But I guess the question you want to ask is: if the parameter list of two methods is exactly the same, can you make them different from their return values to implement overloading Overload .

This is not going to work.

We can use contradiction to illustrate this problem, because we can sometimes call a method without defining the return result variable, that is, do not care about its return result,


For example, we call Map.Remove (Key) method, although Remove method has a return value, but we usually do not define a variable to receive the returned result, assuming that the class has two names and parameter lists in exactly the same way, only the return type is different, Java It is not possible to determine which method the programmer is trying to invoke, because it cannot be judged by the return result type.


Override can be translated as an overlay, literally knowing that it is covering a method and rewriting it to achieve different effects.

The most familiar overlay for us is the implementation of the interface method, which is generally just a declaration of the method in the interface, and we need to implement all the methods of the interface declaration when we implement it.

In addition to this typical usage, we may also overwrite methods in the parent class with the subclass in the inheritance. Be aware of the following points in the overlay:

Features of Override

1, the mark of the method of covering must match with the mark of the method that is covered completely, can reach the effect of coverage;

2. Thereturn value of the overridden method must be the same as the return of the overridden method;

3. Theexception that is thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;

4.The overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten.

Overload may be familiar to us and can be translated as overloads, it means that we can define some methods with the same name, by defining different input parameters to differentiate these methods, and then when called,theVM According to the different parameter styles, choose the appropriate method to execute. The following points are to be noted in using overloading:


Features of Overload

1. You can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must not be the same, such as can be fun (int,float), but not fun (int, int));

2, can not be overloaded by access rights, return type, thrown exception;

3, the method of the exception type and number will not affect the overload;

4, for inheritance, if a method in the parent class is the access permission is PRIAVTE, then it can not be overloaded in the subclass, if defined, but also only defined a new method, but not to achieve the overloaded effect.


Summarize:

Override (override, overwrite) (occurs in subclass and parent class)
1, method name, parameter, return value is the same.
2. The subclass method cannot reduce the access rights of the parent class method.
3. The subclass method cannot throw more exceptions than the parent class method (but the subclass method can not throw an exception).
4, exists between the parent class and the child class.
5. The method is defined as final and cannot be overridden.

overload (Overload, overload) (occurs in the same method)
1, the parameter type, number, order at least one is not the same.
2. You cannot overload a method name that has only a different return value.
3, exists in the parent class and subclass, in the same class.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Override and overload differ, can the overloaded method change the type of the return value?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.