The difference between overloading and resetting (overwriting) methods in Java

Source: Internet
Author: User

In a nutshell, overloading is the same as allowing more than one method of the same name to exist in the same class, as long as the number or type of arguments for these methods is different, and resetting (overwriting) is a subclass that defines a method that has already been defined in the parent class, that is, subclasses overriding the parent class method.

Overloading of methods

The overload of a method is to allow more than one method of the same name to exist in the same class, as long as the number or type of arguments for these methods is different . There are three kinds of situations:

1. The number of parameters is different, which makes up overloading
 Public int Add (int A,int  b) {    return a +b;}  Public int Add (int  a) {    return  A}
2. Different parameter types, which make up overloading
 Public int int A,int  b) {    return a +b;}  Public Double Double A,double  b) {    return a +b;}
3. The parameter order is different, which makes up the overload
 Public int int A,double  b) {    return 1;}  Public int Double A,int  b) {    return 1;}
Note: Although the return values of two methods can be different in a method overload, only the return values are not sufficient to determine whether the overloads are overloaded or not, and are set by the number of arguments and the type.

The difference between overloading and resetting (overwriting) methods in Java

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.