The difference between overloading and overriding in Java

Source: Internet
Author: User

1. Overloading

The same class is allowed for multiple methods with the same name but with a different number of formal parameters or parameter types, and the corresponding method is called according to the formal parameter list when called.

2. Judging

(1): The method name must be the same.

(2): The number of formal parameters or different types of parameters (satisfy one of the conditions on the line).

  

3. Rewrite

The polymorphism between the parent class and the child class, the function of the parent class is redefined, and a method defined in the subclass has the same name and parameters as its parent class, which can be called the method is overridden or overwritten.

4. Judging

(1): Access modifier permissions must be greater than the overridden method. (The overridden method cannot be overridden when it is private).

Permissions: Public>protected>default>private

(2): When the overridden method is final, it cannot be rewritten if the compilation will error.

(3): The return value type must be the same.

(4): The parameter list must be exactly the same as the method being overridden.

(5): The overriding method must not throw a new check exception or a more generic check exception than the overridden method declaration

For example: A method of the parent class declares a check exception ioexception, in overriding this method is not able to throw exception, can only throw IOException subclass exception, can throw non-check exception.

Comprehensive: With the same method name, return type, and parameter table, it will be overridden

Note: You can use the Super keyword, which refers to the parent class of the current class, if you want a method from the parent class

The difference between overloading and overriding 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.