The difference between overload and Override

Source: Internet
Author: User

The difference between overload and Override

Overload is the meaning of overloading, override is the meaning of overrides, that is, rewriting.

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 invoking the method from an instance object created by the subclass invokes the definition method in the subclass, which is equivalent to overwriting the exact same method defined in the parent class. This 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, then the subclass does not have an override 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 the two methods is exactly the same, can you make them different from the return values to implement the overloaded overload. This is not possible, we can use contradiction to illustrate this problem, because we sometimes call a method can also not define the return result variable, that is, do not care about its return results, for example, when we call the Map.Remove (key) method, although the Remove method has a return value, However, we usually do not define a variable to receive the return result, when it is assumed that the class has two names and the parameter list is exactly the same method, only the return type is different, Java will not be able to determine the programmer's bottom is to invoke which method, because it can not 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:

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. The return value of the overridden method must be the same as the return of the overridden method;

3. The exception 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 more familiar to us, can be translated as overloaded, it means that we can define some of the same name, by defining different input parameters to differentiate these methods, and then call, the VM will be based on different parameter styles, to choose the appropriate method of execution. The following points are to be noted in using overloading:

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 differ, for example, 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 private, then it can not be overloaded in subclasses, if defined, but also defined a new method, but not to achieve the overloaded effect.


The difference between overload and Override

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.