Java overwrite and reload

Source: Internet
Author: User

Override refers to redefining the method of the base class so that the method has different meanings in the derived class. "Overload" refers to assigning two definitions to the method name and parsing different parameter lists. The overload can exist in any class, regardless of whether the base class is explicitly specified when the class is defined.
The following explains how to differentiate them: when the "overwrite" method is defined, the new method definition in the derived class has the same number and type. On the other hand, if the method of the derived class has different parameter numbers, or if a parameter type is different from the location of the base class, the derived class will have these two methods. This is "heavy load ". The base class does not need to be involved during overload, as long as two method definitions are provided in the class definition.

 

 

 

Differences between overwrite and reload

Override: overwrite, which overwrites a method and overwrites it for different purposes. What we are most familiar


It is the implementation of the interface method. In the interface, the method is generally declared, and we need


All methods that implement the interface declaration. In addition to this typical usage, we may also overwrite


The method in the parent class.

Pay attention to the following points when overwriting:
1. the logo of the covered method must match the logo of the covered method to achieve the coverage effect;
2. The returned value of the overwritten method must be the same as that of the overwritten method;
3. The exception thrown by the overwriting method must be the same as that thrown by the overwriting method, or its subclass;
4. The method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.

Overload: overload. It means that we can define methods with the same name and distinguish them by defining different input parameters.


Then, the VM selects the appropriate method for execution based on different parameter styles. To


Note the following:
1. You can only use different parameter styles when using overload. For example, different parameter types, different parameter numbers, and different parameter Shun

(Of course, several parameter types in the same method must be different;
2. It cannot be overloaded by access permission, return type, or thrown exception;
3. The method exception type and quantity do not affect the heavy load;
4. For inheritance, if a method has a priavte access permission in the parent class, it cannot be overloaded in the subclass, as shown in figure

If it is defined, it only defines a new method and will not achieve the effect of overloading.

 

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.