Differences between Overloading and rewriting in Java

Source: Internet
Author: User

From: http://blog.csdn.net/zhu_apollo/article/details/1852542

1. overload is a manifestation of the polymorphism of the same method in a class. A function with the same name has different parameter numbers/types (1)
Method Overloading is a means for classes to process different types of data in a unified manner. Multiple Functions with the same name exist at the same time and have different parameter numbers/types. Overload Overloading is a manifestation of polymorphism in a class. (2)
Java method overloading means that multiple methods can be created in the class. They have the same name, but have different parameters and different definitions. When calling a method, the number and type of different parameters passed to them are used to determine which method to use. This is polymorphism. (3)
During overload, the method name must be the same, but the parameter type and number are different. The return value type can be the same or different. Return types cannot be used as the criteria for distinguishing heavy-duty functions. 2. Overriding)If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). (1)
The polymorphism between the parent class and the Child class, and the function of the parent class is redefined. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). In Java, subclasses can inherit the methods in the parent class without re-writing the same method. But sometimes the subclass does not want to inherit the parent class from the original method, but wants to make some modifications, which requires method rewriting. Method override is also called method override. (2)
If the method in the subclass has the same method name, return type, and parameter table as a method in the parent class, the new method overwrites the original method. If you need the original method of the parent class, you can use the super keyword, which references the parent class of the current class. (3)
The access modification permission of subclass functions cannot be less than that of the parent class. in Java, @ override is used to check and override:

If you want to override the method of the parent class, such as the tostring () method, add @ override before the method to be overloaded. In this way, the system can help you check the correctness of the method during compilation.

As follows:

@ Override
Public String tostring () {...} This is correct

If tostring is written as tostring

@ Override
The Public String tostring () {...} compiler can detect that this writing method is incorrect and reminds you to correct it.

If @ override is not added
Public String tostring () {...} so that the compiler will not report an error. It will think that it is the new method you add in the class.

So be careful when programming, not all error systems can find

Reference: 1. http://blog.csdn.net/zhu_apollo/article/details/18525422.http://apps.hi.baidu.com/share/detail/329134103.http://www.iteye.com/problems/854

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.