Overload & amp; Override,

Source: Internet
Author: User

Overload & Override,
Overload & Overrideoverload -- Overload

Method overloading means that multiple methods with the same name but different parameters can be defined in a class. During the call, the corresponding method is selected based on different parameter tables.

Rule: two are different from three.
Two identical: the same class and the same method name
Three different: the parameter type, number, and order are different.

Note: different parameter names and different return values cannot constitute a method overload. If only the method parameter names are different or the method return value is different, the real parameters you enter when calling the method correspond to multiple methods in the parameter list, the compiler cannot know which method you actually call.

The code is described as follows:

Note: Any method overload, including constructor, is allowed in java. Therefore, to completely describe a method, you need to point outMethod NameAndParameter typeThis is called the signature of a method ).

Override ----- rewrite

Modify the methods inherited from the parent class as needed in the subclass. This is called method rewriting, or method overwrite. The override method can only exist in an inherited relationship.

Note:

(1) The subclass cannot override the Methods Modified by the keyword final in the parent class (including the private method, because the private method is implicitly final ).

(2) The rewrite method must have the same method name, parameter list, and return value type as the method to be rewritten.

(3) the method to be rewritten cannot use more strict access permissions than the method to be rewritten (because of polymorphism ). Reload.

(4) A non-checked exception can be thrown if the method to be rewritten does not throw a new exception or a broader inspection exception than the declared method to be overwritten. For example, a method of the parent class declares an IOException check. When rewriting this method, the rewrite method cannot throw an Exception, but can only throw an IOException or its subclass Exception, you can also throw a non-checked exception. You can throw different exceptions for a reload.

Method overloading does not matter with method rewriting!

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.