Java overload and override

Source: Internet
Author: User

Overriding and overloading are different manifestations of Java polymorphism.
Overriding is a manifestation of polymorphism between parent class and Child class, while Overloading is a manifestation of polymorphism in a class.

If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten)
. When a subclass object uses this method, the definition in the subclass is called. For it, the definition in the parent class is "blocked.

If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types.
Is called the method overload ). Exceptions cannot be thrown through access permissions, return types, or other methods;
.
1. Override features
1. Mark of the covered Method Required Identifier of the method to be overwritten Exact match To achieve the coverage effect;
2. Return Value of the override method Required It is consistent with the returned result of the overwritten method, or its subclass;
3. The exception declaration thrown by the method has no effect on overwrite, that is, the subclass can declare any number or type of exceptions. (exception is a bit special)
4. The method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.
2. Overload features
1. When heavy load is used Only Use different parameter styles. For example, different parameter types, different parameter numbers, and different parameter Order (of course, several parameter types in the same method must be different, such as fun (INT, float ), but cannot be fun (INT, INT ));
2, It cannot be overloaded by access permission, return type, or thrown exception;
3. The method exception type and quantity do not affect the heavy load;

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.