Java overloads (Overload) and overrides (Overwrite) Summary

Source: Internet
Author: User

The content source is: "Sun Weichen Object-Oriented Programming", this essay brief summary, the concrete content can see the outline 6th Chapter, write very clear;

I. Overloading (OVERLOAD)

1. Sometimes the same function of a class has multiple implementations, whichever implementation is used, depending on the parameters given by the caller ( such as a constructor is an example );

2. For a method of a class (including a method inherited from a parent class), if there are two methods with the same method name, but the parameters are different, one method is the overloaded method of the other method.

3. The overloaded method must meet the following conditions :

   (1) The method name is the same;

(2) The parameter type, number and order of the method are at least one different;

(3) The return value type of the method can be different;

(4) The modifier of the method can be different;

4. In a class, it is not allowed to define two method names that are identical, and parameter signatures are identical. Because if such a method exists, the Java Virtual Machine cannot at runtime decide which method to execute, and the parameter signature refers to the type, number, and order of the parameters.

Two. Rewrite (OverWrite)

1. If a method is defined in a subclass, whose name, return value type, and parameter signature match exactly the name of a method in the parent class, the return value type, and the parameter name, you can say that the method of the subclass overrides (Overrides) the method of the parent class;

2. The overriding method must meet the following constraints:

(1) The name of the method of the subclass, the parameter signature and the return value type must be the same as the name of the parent class method, the parameter signature, and the return value type;

(2) When overriding a parent class method, the overriding method cannot narrow the access rights of the parent method (because of a conflict with the polymorphic mechanism, the details can be seen in Java object-oriented programming);

(3) The subclass method cannot throw more exceptions than the parent class method, and the subclass method throws an exception that must be the same as the exception thrown by the parent class method, or the exception that the subclass method throws is the subclass of the exception thrown by the parent method (because of a conflict with the polymorphic mechanism, which can be seen in Java object-oriented programming);

(4) method overrides exist between subclasses and parent classes, including direct and indirect parent classes, where methods can only be overloaded and cannot be overridden.

(5) The static method of the parent class cannot be overridden by the quilt class as a non-static method;

(6) Subclasses can define static methods with the same name as static methods of the parent class to hide static methods in the parent class in subclasses;

(7) The non-static method of the parent class cannot be overridden by the quilt class as a static method;

(8) A private method in the parent class cannot be overridden by a class, because the subclass method overrides the parent class method, so that the subclass must be able to inherit the specific method of the parent class, so that the private method in the parent class cannot inherit the quilt class, nor can it be overridden, even if it is formally conforming to the overridden specification;

(9) The abstract method of the parent class can be overridden in two ways, one is to implement the abstract method of the parent class, and the other is to re-declare the abstract method of the parent class.

(10) A non-abstract method of the parent class can be overridden as an abstract method.

Java overloads (Overload) and overrides (Overwrite) Summary

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.