Java method overloading and method Rewriting

Source: Internet
Author: User

Method Overload ):

1. In the same class

2. Same method name

3. the number or type of parameters are different.

4. It is irrelevant to the return type of the method.

5. It is irrelevant to the modifier of the method.

 

Override ):

Method override must be a subclass that inherits the parent class to override the method of the parent class.

1. The method to be rewritten must have the same method name as the method to be rewritten.

2. The override method and the override method must have the same parameter list.

3. the return value type of the override method must be the same as the return value type of the method to be overwritten.

4. The access permission of the method to be rewritten cannot be reduced for the method to be rewritten.

Note that the override method must be the method of the parent class that can be accessed in the subclass.

 

Differences between method overloading and method Rewriting:

1. the overload is a method of the same name in the same class. The method name must be the same and the parameter list must be different, regardless of the type of the returned value.

2. Rewriting involves a method with the same name between the subclass and the parent class. The method name must be the same, the parameter list must be the same, and the return value type must be the same.

 

Method overload example:

Work (x, work () {0 work () {} View Code

If the test result is displayed, use this key plus. overload method directly.

 

Example of method Rewriting:

Child "nihao" View Code

 

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.