Java method overload and method overlay differences __java

Source: Internet
Author: User
the difference between method overload and method overlay:
Method Overload: Overload
Method Overlay: Override
there is no relationship between the two itself, just because the name is very similar.


method Overload: Overload
Function:
Solves the problem that the same function method name is different in the same class, since it is the same function, then the method name should be the same
rule: (Two the same difference)

In the same class, the method name is the same, the method parameter list is different (parameter type, number of parameters, parameter order)


The following figure is the method overload in the JDK's own tool class arrays: you can see that the method name is the same, but the parameters are different





Method Overlay: Override
Function:
Once a subclass inherits a parent class, it is possible that a method of the parent class does not meet the specific characteristics of the subclass, at which point you need to redefine the method in the subclass and override the method body.

method overrides: when subclasses extend the parent class, you can get some of the methods and member variables of the parent class. However, when a method of a parent class does not fit the characteristics of the subclass itself, then the subclass needs to redefine the method of the parent class and override the method body, which is called the method overlay (Override).

rule: two small and one big together
Together:
1, the instance method signature must be the same. (Method signature = Method name + method argument list)
Two small:
1. The return value type of a subclass method is the same as the return value type of the parent class method or its subclass, and the subclass can return a more specific class;
2. The exception type thrown by the subclass method declaration is the same as the exception type or the subclass of the parent class method declaration.
One big:
1. The access rights of a subclass method are greater or equal than the access rights of the parent-class method. (The private-decorated method cannot inherit from the quilt class, nor does it have a overridden concept).

To identify whether the method is to override the technique: @Override tags, if the method is overridden, in the method affixed to the label, compiled through; otherwise, compilation error. only methods have the concept of overriding, and fields are not overwritten.

The following figure is a simple example of method overlay: refer to


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.