Differences between Java overrides and overloads

Source: Internet
Author: User

overriding : Overrides are subclasses of methods that override the parent class, with two important features

1. Subclass method Name and parameter type, number must be the same as the parent class.

2. If there is a return value, the return value type must also be the same as the parent class.

1 //Parent Class2  Public classBird () {3     PublicString GetInfo () {4     return"GetInfo method in parent class";5     }6  }7 8 //subclasses overriding the GetInfo method of the parent class9 Ten  Public classLittlebirdextendsbird{ One @Override A       PublicString GetInfo () { -      return"GetInfo Methods in subclasses"; -     } the}

overloading : Overloading is to allow a method to have multiple implementations in a class, with different parameter types and numbers.

When overloaded. The method name must be the same, the number of parameter types and the return value are not required.

1  Public classplus{2      Public intPlusintAintb) {  3         returnA +b;}4      Public intPlusintAintBintc) {//Plus method overloads5         returna+b+C;}6      Public DoublePlusDoubleADoubleb) {//Plus method overloads7         returnA +b;}8     9     Public Static voidMain (String args[]) {TenPlus Twoint =NewPlus (); OnePlus Threeint =NewPlus (); APlus twodouble =NewPlus (); -SYSTEM.OUT.PRINTLN ("Call is the Plus Method 1, the result is:" +twoint.plus (5,6));//the plus method is called by default when the parameter is two int values 1 -SYSTEM.OUT.PRINTLN ("Call is the Plus Method 2, the result is:" +twoint.plus (5,6,8));//the plus method is called by default when the parameter is three int values 2 theSYSTEM.OUT.PRINTLN ("Call is the Plus Method 1, the result is:" +twoint.plus (5.42,6.62));//the default call to the Plus Method 3 when the parameter is a two double value -    } -}

The difference between overrides and overloads is that:

Overriding polymorphism works, and calling overloaded methods can greatly reduce the amount of code input, and the same method name can have different functions or return values as long as it passes different parameters inside.

With good rewriting and overloading, you can design a clear and concise class that can be said to be overridden and overloaded in a way that is very unusual in the process of writing code.

Differences between Java overrides and overloads

Related Article

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.