"Java" Overloads (Overload) and overrides (override)

Source: Internet
Author: User

Syntax of the method

modifier Returns a value type method name (parameter type argument name) {    ...    Method Body    ...     return return value;}

Overloading (overload)

/** * Overloaded Overload: * In the same class, multiple methods have the same name, but the parameter types and the number of parameters must be different. * The return value may or may not be the same, but the return value alone cannot be used to determine whether a method exists. */public class Example {public void print () {};//public String print () {}; The parameter type is the same as the previous method, the return value is different and cannot be compiled by public void print (int A) {};p ublic void print (String a) {};}

overriding override

/** * 1. Overriding methods can only exist between parent-child classes, and the parameter list and return type of this method between parent and child classes must be exactly the same * 2. In the overridden method, the permission modifier for the subclass must >= the * 3 of the parent class. When the parent class method is private, the subclass has a method that has the same name that is not part of the method's overwrite, but a method that is redefined inside the subclass * 4. In the overridden method, the subclass throws an exception that requires less than or equal to the exception thrown by the parent class * */class animal{int num;public void Eat () {System.out.println ("fruits");}; private void Run () {System.out.println ("Run");} public void count () throws IOException {}}class lion extends animal{public void eat () {//belongs to overwrite System.out.println ("meat");} public void Run () {//does not belong to the rewrite System.out.println ("Jump");} public void count () throws classnotfoundexception {}; Cannot throw exceptions that are not in the parent class method public void count () throws IOException {}//subclass throws an exception that can be the same as the parent class exception or child exception}

Reference:

Https://www.cnblogs.com/jenkinschan/p/5847470.html

"Java" Overloads (Overload) and overrides (override)

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.