overriding and overloading of Java methods

Source: Internet
Author: User

Method Rewrite rules:

The parameter list and the return value type must be the same as the overridden method

Access permissions cannot be lower than the overridden methods in the parent class. For example, if a method of the parent class is declared public, overriding the method in the subclass cannot be declared as protected

The Member method of the parent class can only be overridden by its subclasses

A method that is declared final cannot be overridden

static methods can also be inherited However, static methods cannot be overridden
can
: parent class name . static method Name (superclassnamestaticmethodname) call hidden
static method

Subclasses and parent classes in the same package, subclasses can override all methods of the parent class, except for the methods declared private and final.

Subclasses and parent classes are not in the same package, subclasses can only override non-final methods that are declared public and protected by a parent class

The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method declaration, or vice versa.

The construction method cannot be overridden.

If you cannot inherit a method, you cannot override this method

Method overload rules:

Overloading (overloading) is a class in which the method name is the same and the parameters are different. Return types can be the same or different

The overloaded method must change the parameter list (the number of parameters or the type or order is different);

Overloaded methods can change the return value type

Overloaded methods can change the access modifier

Method can be overloaded in the same class or in a subclass

  

  

overriding and overloading of Java methods

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.