Differences between rewriting and overloading in Java

Source: Internet
Author: User

1. Overriding

Overriding overwrites a method to achieve different effects. (That is, the tone must be the same) for us, the most familiar coverage is the implementation of the interface method. In the interface, it is generally only the declaration of the method, and when we implement, all methods declared by the interface must be implemented. In addition to this typical usage, we may also overwrite the methods in the parent class in the subclass.
The main features of rewriting are: 1.The method name must be the same as the method to be rewritten. 2. The list of method parameters must be the same as that of the method to be overwritten. 3. The return type must be the same as the method to be overwritten. 4. The override method must provide greater permissions than the method to be overwritten. E.g. if the override method is protected void method (int I, Double J), the override method can be protected void method (int I, Double J) (generally ), or public void method (int I, Double J), but it cannot be private void method (int I, Double J) or the default package... 5. A new exception or a wider exception cannot be thrown. 6. The method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten. 2. OverloadingOverloading means that we can define methods with the same name. We can define different input parameters to differentiate these methods (in different tone) and then call them again, JVM selects appropriate method execution (polymorphism) based on different parameter styles ).
The main features are as follows: 1. The method names for each overload are consistent. 2. The parameter list of each overload method is different (including one or more of the three parameters in the parameter type, number of parameters, and Parameter order ).
3. Any return type. (The return value of a method cannot be used to distinguish between overloaded methods .) 4. Any access controller. (You cannot distinguish between overloaded methods by their access permissions .) 5. You can throw your own exceptions, regardless of the overloaded method. (The overload method cannot be distinguished by the thrown exception .)

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.