[Concept] method overloading and rewriting

Source: Internet
Author: User
Method overloading and rewriting

1. Method Overloading

 

Method overloading means that the same method in the class has different implementation forms, depending on the parameters specified by the caller. Different parameters are given, and different methods are called.
Note that the method name, Parameter order, and type constitute the signature of the method. In the class, the signature of each method must be unique, and the compiler is the root

The method signature is used to determine the method to be called, but the return type does not affect the method signature.

 

2. Method Rewriting

 

Method rewriting occurs only when the child class inherits the parent class. When the subclass inherits the parent class, we can directly use all the methods in the parent class, but sometimes the methods in the parent class cannot fully meet our needs, we can change the method of the parent class appropriately, which is called method rewriting.

Sometimes it is also called method overwrite by mistake (but it is not really covered, so it is somewhat far-fetched ).

 

We need to note that there are some differences between method rewriting and overwriting:

A. Method rewriting requires that the method signature be the same, while the method overloading requires that the method signature be different.

B. The return types of method rewriting must be consistent, while the method overload is not required.

 

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.