Overloading and overwriting

Source: Internet
Author: User

When it comes to overloading and overwriting in Java, we're all familiar with it, but I'm going to write about it today.

Topics in this article:

I. What is overloading

Two. What is coverage

Three. The difference between the two

overloaded (overload):

In a class, if two or more than two functions of the same name appear, as long as the number of their arguments, or the type of the parameter is different, it can be called the function overload.

That is , when the function has the same name, it only looks at the argument list. And the return value type is okay.

You need to be careful when using heavy loads:

1. You can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order.

2. The exception type and number of methods do not affect overloading.

3. For a parent class, a subclass can overload a method that has the same name as an inherited method in the parent class, and if a method is access-PRIAVTE in the parent class, it cannot be overloaded in subclasses, and if defined, it simply defines a new method without overloading the effect.

cover (override):

When the same method appears in the child parent class, creating a subclass object runs the method in the subclass. As if the methods in the parent class were overwritten. So this situation is another feature of the function: overwrite.

When do I use overlays?

When a subclass inherits the parent class, the function content of the parent class needs to be modified, which can be overridden to implement the

When the method is covered, note the two point:

1. When a subclass overrides a parent class, it must be ensured that the permissions of the subclass method must be greater than or equal to the parent class method permission to implement inheritance. Otherwise, the compilation fails.

2, when covered, either static, or not static. (Static can only be overridden by static, or statically overwritten) YES

  3. The final modified method is the ultimate method and cannot be overwritten.

4. When a subclass overrides a method of a parent class, it can only throw fewer exceptions than the parent class, or a child exception that throws an exception thrown by the parent class, because subclasses can solve some problems with the parent class and cannot have more problems than the parent class.

5. The subclass method can be accessed only larger or equal than the parent class, and cannot be smaller. If the method of the parent class is private, then the subclass does not have an override limit, which is equivalent to adding a new method to the subclass.

The difference between overloading and overwriting:

1, the method's coverage is the relationship between the subclass and the parent class, is the vertical relationship, the overloading of the method is the relationship between the methods in the same class, and is the horizontal relationship.
2. Overrides can only be made by one method, or only by a pair of methods; an overload of a method is a relationship between multiple methods.
3. The override requires the same parameter list, and the overload requires the parameter list to be different.
4, in the coverage relationship, call that method body, is based on the object type to determine, overload relationship, is based on the invocation of the actual parameter table and formal parameter list to select the method body.

Overloading and overwriting

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.