The difference analysis of overload, overlay and hide in Java _java

Source: Internet
Author: User

Overload: The method name is the same, but there are multiple functions of the same name with different parameters

Note: 1. Different parameters mean the parameter type, the number of parameters, the order of the parameters at least one different

2. return values and exceptions, and access modifiers, cannot be used as overloaded conditions (because of ambiguity in anonymous invocation, eg:void A () and int A (), if a () is invoked, ambiguity)

The 3.main method can also be overloaded

Overrides: Subclasses override methods of the parent class, requiring the method name to be exactly the same as the parameter type (arguments cannot be subclasses), the return value and the exception are smaller or the same than the parent class (that is, subclasses of the parent class), and the access modifier character parent class is large or the same

   Two small and one big

Note: The subclass instance method cannot overwrite the static method of the parent class, nor can the static method of the subclass overwrite the instance method of the parent class (compile-time error), summarized as the method cannot cross overlay

Hiding: A property or method with the same name as a parent class and a subclass has the same property or method of the parent class that is not formally visible

Note: When shadowing occurs, the class is called, and the property or method of the corresponding class is invoked without dynamic binding

Method hides only one form, that is, the same static method exists for the parent class and the subclass

     Properties can only be hidden and cannot be overwritten

Subclass instance variable/Static variable can hide instance/static variable of parent class, sum up as variable can be hidden

The difference between hiding and overriding:

A hidden property that accesses a property in the parent class after the subclass is cast to the parent class

The overridden method, when a subclass is coerced into a parent class, calls a method of the subclass itself

Because overlays are dynamic bindings, are constrained by Rtti (run time type identification, run-time type checking), hidden from rtti constraints, summarized as Rtti only for overrides, not for hidden

Special case:

1.final-decorated properties can be hidden, but can not be assigned value, that is, can not use = To assign a value, the net that the final attribute can not be modified, this is not accurate, because for a reference type of variable with final modification, it just can't be pointed to other objects, but it could change its own value, You can use the ArrayList test, the final property can be initialized at run time, but the initialization statement cannot be found

The 2.final decorated method cannot be overridden and can be overloaded

3.final decorated class cannot be inherited

The 4.private method implicitly adds final

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.