Java overloading and coverage

Source: Internet
Author: User

Sometimes, the same function of the class has multiple implementation methods. The implementation method depends on the parameters specified by the caller. For example, the most common System. out. println () can print data of any data type. It has multiple implementation methods. During runtime, the Java Virtual Machine first determines the type of the given parameter, and then determines which println () method to execute (int char String, etc ).

 

Overload: For Class methods (including methods inherited from the parent class ),Same method name,Different parameter listsMethods constitute a heavy load relationship. Note the following two problems:

(1) What is a parameter list? The parameter list is also called a parameter signature. It refers to three items:Parameter type,Number of parameters,Parameter order. If there is a difference between the three parameters, the parameter list is different.

(2) can an overload relationship only occur in the same class? None. At this time, you need to deeply understand inheritance. You need to know that the members of a subclass are inherited by the parent class in addition to those explicitly written by themselves. So a method andMethods inherited from the parent classYou can also overload the link.

You needClosely definedTo check whether there is a heavy load relationship between methods. You don't need to worry about the modifier, return type, and thrown exception of the method. You only need to check the method name and parameter list. Remember that the constructor can also be reloaded.

 

Override (override): This is also called overwrite. When some methods in the parent class cannot meet the requirements, the method of the parent class is rewritten in the subclass. When a method in the parent class is overwritten, the method in the parent class cannot be called unless the super keyword is used.

Conditions for overwriting:

1."Three identical and not low"Subclass and parent classMethod Name,Parameter List,Return typeRequiredIdenticalAnd the access modifier of the subclass method cannot be higher than that of the parent class.Low.

2. Subclass methods cannot throw more than parent MethodsMore exceptions. That is, the exception thrown by the subclass method must be the same as that thrown by the parent method.Exception consistency, OrIs its subclass, OrNo throws;

3. The method to be overwritten cannot beFinalType. Because the final modification method cannot be overwritten.

4. The method to be overwritten cannot bePrivate. Otherwise, only a new method is defined in its subclass and it is not overwritten.

5. The method to be overwritten cannot beStatic. Therefore, if the methods in the parent class are static and the methods in the subclass are not static, but both methods satisfy the overwrite conditions, a compilation error will occur. And vice versa. Even if the methods in the parent class and subclass are static and meet the overwriting conditions, they will not be overwritten, because the static method matches the static method with the reference type of the class during compilation.

 

Method coverage and overloading share the following points:

Both require the same method name

Can be used between abstract methods and non-abstract methods.

 

Method coverage and overloading have the following differences:

Method override requires that the parameter list (parameter signature) be consistent, and method overload requires that the parameter list be inconsistent.

Method override requires that the return types be consistent, and method overloading does not.

Method override can only be used for sub-classes to overwrite the parent class. Method Overloading is used for all methods in the same class (including methods inherited from the parent class)

Method override has special requirements on method access permissions and thrown exceptions, and method overloading has no restrictions in this regard.

A Method of the parent class can only overwrite the quilt class once, and a method can be reloaded multiple times in all classes.

 

In addition, attributes (member variables) cannot be overloaded and can only be overwritten.

Transferred from:

Http://blog.csdn.net/lenotang/article/details/2681525

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.